Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim 3 blackholing errors

Tags:

slim

I have a small slim 3 app, and when I throw an exception slim simply shows the generic error message:

Slim Application Error

A website error has occurred. Sorry for the temporary inconvenience.

In slim 2 you can do something like this to turn on debug mode giving you backtraces etc:

$app->config('debug', true);

In slim 3 there doesn't seem to be one. Additionally, it seems to be overriding my exception and error handlers.

How can I get slim to spit out errors, or at least to call my error handlers (Which pipe the output to kint for debug information)

like image 856
J V Avatar asked Dec 02 '15 18:12

J V


4 Answers

Looking through the source, it's possible to initialize slim 3 with error display like so:

$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);

I'm not sure if it's possible to change this setting after the fact without replacing the errorHandler altogether.

like image 107
J V Avatar answered Nov 05 '22 01:11

J V


To show full stack trace on default exception handler use what j-v said.

If you want to handle exceptions in Slim yourself then you need to override Slim's default exception handler as it will be used before your "not in Slim" error handler:

$app = new \Slim\App();

$container = $app->getContainer();
$container['errorHandler'] = function(ServerRequestInterface $request, ResponseInterface $response, Exception $exception) {
    //Handle exception here
}
like image 28
Julián Gutiérrez Avatar answered Nov 04 '22 23:11

Julián Gutiérrez


Error handling is rather well documented: Official Docs

$app = new \Slim\App();
$c = $app->getContainer();
$c['errorHandler'] = function ($c) {
    return function ($request, $response, $exception) use ($c) {
         return $c['response']->withStatus(500)
                              ->withHeader('Content-Type', 'text/html')
                              ->write('Something went wrong!');
         };
   };
like image 2
BzK Avatar answered Nov 04 '22 23:11

BzK


Error handling is best solution to this. You can do something like to see Error Trace

$app = new \Slim\App();
$container = $app->getContainer();
$container['phpErrorHandler'] = $container['errorHandler'] = function ($c) {
    return function ($request, $response, $exception) use ($c) {
         return $c['response']->withStatus(500)
                              ->withHeader('Content-Type', 'text/html')
                              ->write('Something went wrong!<br><br>' . 
                                      nl2br($error->getTraceAsString()));
         };
   };
like image 1
Riz Avatar answered Nov 05 '22 00:11

Riz