1

Follow-up on unanswered questions Custom ExceptionListener picks up 403 & 404, but not 500 errors and Symfony 2.4: Why are 500 errors not caught by kernel.exception listener.

I have implemented a custom exception listerner which works brilliantly for 403 and 404 errors, but does not catch 500 errors. Some feedback I have gotten indicates that some 500 errors occur before Symfony has a chance to run and therefor cannot be caught by any listeners.

What I have noticed is that no matter what type of strange error it is that I generate, whether it is a syntax error, undefined variable, unknown method, method call on a non-object, etc, it seems that ErrorHandler->handle() or ErrorHandler->handleFatalError() is always being called in the development environment (see some samples below) (While the same is not true for the development environment, where I often get a normal PHP error not inside Symfony).

What I am trying to do is that, whenever an error like this occurs in the production environment, I ALWAYS show a user friendly (Oops, something went wrong) error page and ALWAYS send an email with the error specifics. To me it seems that if I could customize ErrorHandler->handle(), then I would be able to accomplish this. The reason for this is simple, I want to be made aware of any errors that occur to my systems that are in production. One would think that this is a relatively common feature and yet I am unable to do it.

I am still learning Symfony and even though I have implemented custom console commands, custom authentication handlers more of the complex security features, I am really struggling with this. I would really appreciate if someone with some extensive Symfony experience could tell me if this is possible or not and, if so, how I would go about doing it.

enter image description here

enter image description here

enter image description here

2 Answers 2

2

You will need to enable debugging in your production environment. Use Debug::enable() in your app.php. Refer the symfony blog. For making the errors come to you via email, monolog can be used. You need to register logger for channel "emergency" with appropriate configuration for sending mail. This is the channel used for logging via error handler.

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

Comments

1

Actually I had the same problem and came up with a solution. I'm not sure if it is the best one, but it works for me at the moment.

I have added following lines into the app.php (on top of app.php after the "use"-Statment):

register_shutdown_function(function()
{
    $error = error_get_last();
    if ($error != null)
    {
        $pageURL = 'http';
        if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"]; 
        }
        header("Location: ".$pageURL."/senderrorrequest?message=".$error['message']);
    }
});

On this way the user will be redirected to a support-form and the error is logged in the error.log. If anybody has a better solution, I'm happy to read it.

Comments

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.