I've read http://www.slimframework.com/docs/handlers/error.html then using post app method to activate error handler .
$container['errorHandler'] = function ($container) {
return function ($request, $response, $exception) use ($container) {
return $container['response']->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('Something went wrong!');
};
};
Then i try to make some syntax errors and connecting to database error. But Slim 3 Error Handler just not work, no 'Something went wrong!' response, just silent. Why?
Thank you
This works for me:
$app = new 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!');
};
};
$app->get('/', function () {
throw new Exception();
});
$app->run();
This will of course only work for exceptions, not for syntax errors.
AFAIK there is no way to catch Fatal's.
It's not possible to catch fatal errors, but it's possible to log them using register_shutdown_function. Some frameworks uses the shutdown to handle this kind of errors. Symfony, for example, has a special handler for it: ErrorHandler.php
I've been using Slim 3 with the PHP 7 RC 4 run-time and have not run into any issues other than PHP's awful error handling.
PHP 7 was supposed to implement a Throwable and Error interface that allowed a Try...Catch to work with fatal errors (see: https://wiki.php.net/rfc/throwable-interface).
At first I thought Slim was freezing up on handling these, but I think this is a PHP 7 issue.
I would welcome documentation showing how to use register_shutdown_function to render an error. I'm not in favour of making this a default in the code though.
The problem with fatal errors is that the application is in a faulted state.
So if you were to include Slim code to try to send a JSON payload back to the client or even HTML it may not work and can throw more errors.
Even wrapping a Slim Response Send in try...catch when the application is in a faulted state will not prevent an error from being thrown and then the error from the error from the error recursion situation can happen. One of the reasons that the Slim framework should avoid including this from their base code.
Anyway, here's an example of capturing fatal errors in PHP:
<?php
global $fatality;
class Fatality
{
public function __construct()
{
register_shutdown_function( [$this, "fatal_handler"]);
}
public function fatal_handler()
{
// Determine if there was an error and that is why we are about to exit.
$error = error_get_last();
// If there was an error then $error will be an array, otherwise null.
if ($error !== NULL)
{
echo 'An unrecoverable error has occurred' . PHP_EOL;
echo 'Error code: ' . (string)$error['type'] . PHP_EOL;
echo 'Message: ' . $error['message'] . PHP_EOL;
echo 'File: ' . $error['file'] . PHP_EOL;
echo 'Line: ' . (string)$error['line'] . PHP_EOL;
exit($error['type']); # Exit with the error type code.
}
}
}
$fatality = new Fatality();
Thanks a million for saving my time.
Most helpful comment
The problem with fatal errors is that the application is in a faulted state.
So if you were to include Slim code to try to send a JSON payload back to the client or even HTML it may not work and can throw more errors.
Even wrapping a Slim Response Send in try...catch when the application is in a faulted state will not prevent an error from being thrown and then the error from the error from the error recursion situation can happen. One of the reasons that the Slim framework should avoid including this from their base code.
Anyway, here's an example of capturing fatal errors in PHP: