Improved error handling in Slim 3.2.0
We released Slim 3.2.0 yesterday which includes a number of minor bug fixes since 3.1.0 and also a few nice improvements around the way we handle errors.
Slim has a simple exception handler implemented that displays an error page that looks like this:
It’s not very informative, is it? That’s intentional as we don’t want to leak information on a live website. To display the error information you need to enable the displayErrorDetails setting like this:
$config = [ 'settings' => [ 'displayErrorDetails' => 1, ], ]; $app = new \Slim\App($config);
Not too difficult, if you know the setting! If you don’t then you’re staring at a blank page and have no idea what to do next.
To help solve this Slim 3.2.0 now writes the error information to the PHP error log when the displayErrorDetails is disabled. The built in PHP web server writes the error log to stderr, so I see this in my terminal:
As you can see, all the information needed to find the issue is there, so the developer can get on with her day and solve the problem at hand.
One of the new features of PHP 7 is that it can throw Error exceptions which can then be caught and processed as you would with a standard Exception. However, Slim 3’s error handler is type hinted on Exception and so doesn’t catch these new Errors.
To resolve this, Slim 3.2 ships with a new PHP 7 error handler which works exactly like the current exception handler but it catch Error. Here’s an example (with displayErrorDetails enabled!):
I’m very happy to have more robust error handling in Slim as I think good errors as key for usability and makes Slim that much easier and enjoyable to use. If you find any error situations in Slim that you feel could be improved, please raise an issue.