2

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.

Mark
  • 1,258
  • 13
  • 25

2 Answers2

2

I had this same issue and was slightly led astray by the Slim 3 documentation which mentions the phpErrorHandler feature. However, phpErrorHandler was not added into Slim until version 3.2.0: https://github.com/slimphp/Slim/releases/tag/3.2.0

If you are using Slim version < 3.2.0, try upgrading.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

maybe try to disable display_startup_errors ?

my config:

error_reporting(E_ALL);
ini_set("display_errors", 0);
ini_set('display_startup_errors', 0);

EDIT: also die('oops'); is not an error, it will just die/exit execution of script, nothing after that will be executed

jDolba
  • 391
  • 1
  • 8