0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]);
});

3 Comments

I don't think calls to trigger_error("An error has been triggered"); would be handled properly with this solution.
@JVerstry Well! You didn't mentioned that in question beforehand. But this the legitimate way of handling errors in Symfony and I personally using it in my app. Btw, you could use Exceptions in that case.
PHP's trigger_error() goes hand in hand with set_error_handler()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.