How to set a error handler safety net in Symfony 4?

PHP offers the possibility to set customer error handlers with set_error_handler("customError",E_USER_WARNING); (for example).

How and where can I set some default error handlers for a Symfony 4 application to make sure I catch all unhandled errors?

I have noticed the Debug component which mentions ErrorHandler::register();. Is this the safety net I am looking for? If yes, where should I put that error handler registration call in my code? Should I modify the index.php page?

Upvotes: 0

Views: 1959

Answers (2)

Kafei59
Kafei59

Reputation: 11

Do the kernel events may be a solution to your issue ?

http://symfony.com/doc/current/event_dispatcher.html

You can check out all requests made on your kernel or controllers and even stop propagation of them.

Upvotes: 1

Amit Merchant
Amit Merchant

Reputation: 1043

In your index.php, you can use it like below:

$app->error(function (\Exception $e, $code) use ($app) {
    $errors = [
        [
            'status' => $code,
            'detail' => $e->getMessage()
        ]
    ];
    if ($app['debug'] === true) {
        $errors['file'] = $e->getFile();
        $errors['line'] = $e->getLine();
    }
    return new JsonResponse(['errors' => $errors]);
});

Upvotes: 0

Related Questions