I'm having trouble getting the phpErrorHandler to work. I'm using the latest version of Slim and my server is running PHP 7.1.
use \Slim\App as App;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new App([
'errorHandler' => function ($c) {
return function ($request, $response, $error) use ($c) {
return $c['response']
->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('application error');
};
},
'phpErrorHandler' => function ($c) {
return function ($request, $response, $error) use ($c) {
return $c['response']
->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('runtime error');
};
}
]);
When I throw an Exception from my code, the error handler is invoked and my output is "application error". However, when I throw an Error from my code or introduce syntax errors, the phpErrorHandler is not invoked and, depending on my settings, PHP produces an error message with a stacktrace.
I've tried to add
ini_set('display_errors', 0); // do not produce the standard output when an error occurs
error_reporting(-1); // enable error reporting (is this required for Slim?)
But any changes that I make in that configuration doesn't seem to affect whether or not my closure is invoked.
-edit-
I tried to execute a piece of Javascript that logs to the console. That works for the errorHandler, not for the phpErrorHandler. Also a simple
die('oops');
doesn't do anything in phpErrorHandler. It appears that the callback isn't invoked at all.
If I put a try/catch around
$app->run();
I am able to catch errors. However, this causes some nasty side-effects that I prefer to avoid. What that tells me though, is that it's not a config setting that keeps me from catching errors.