Search code examples
laravellaravel-5laravel-5.1

Add custom 500 error page only for production in Laravel


I want to have a custom 500 error page. This can be done simply by creating a view in errors/500.blade.php.

This is fine for production mode, but I no longer get the default exception/ debug pages when in debug mode (the one that looks grey and says "Whoops something went wrong").

Therefore, my question is: how can I have a custom 500 error page for production, but the original 500 error page when debug mode is true?


Solution

  • I found the best way to solve my problem is to add the following function to App\Exceptions\Handler.php

    protected function renderHttpException(HttpException $e)
    {
        if ($e->getStatusCode() === 500 && config('app.debug')) {
            // Display Laravel's default error message with appropriate error information
            return $this->convertExceptionToResponse($e);
        }
        return parent::renderHttpException($e); // Continue as normal 
    }
    

    Better solutions are welcome!