8
votes

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?

7
can you make a screenshot what do you get on error 500 when you are in production modelewis4u
if(env("APP_ENV") == "production"){ abort(500); } is a pretty simple approach. abort($code) returns the views found in errors/{code}.blade.phpTim Lewis
@lewis4u For production mode I just want to use my custom 500 page. Doesn't matter how it looks. But for debug mode, I want to use Laravel's default page which looks grey and has a bunch of error information on it.Yahya Uddin

7 Answers

7
votes

Simply add this code in \App\Exceptinons\Handler.php:

public function render($request, Exception $exception)
{
    // Render well-known exceptions here

    // Otherwise display internal error message
    if(!env('APP_DEBUG', false)){
        return view('errors.500');
    } else {
        return parent::render($request, $exception);
    }
}

Or

public function render($request, Exception $exception)
{

    // Render well-known exceptions here

    // Otherwise display internal error message
    if(app()->environment() === 'production') {
        return view('errors.500');
    } else {
        return parent::render($request, $exception);
    }
}
6
votes

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 && env('APP_DEBUG') === true) {
        // 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!

4
votes

From Laravel 5.5+ this will now occur automatically if you have APP_DEBUG=false and have a views/errors/500.blade.php file.

https://github.com/laravel/framework/pull/18481

3
votes

Add the code to the app/Exceptions/Handler.php file inside the Handler class:

protected function convertExceptionToResponse(Exception $e)
{
    if (config('app.debug')) {
        return parent::convertExceptionToResponse($e);
    }

    return response()->view('errors.500', [
        'exception' => $e
    ], 500);
}

The convertExceptionToResponse method gets right such errors that cause the 500 status.

1
votes

In the Laravel 7+, you can do the following method

public function render($request, Throwable $exception)
    {
        if (!env('APP_DEBUG', false)) {
            return response()->view("error500");
        } else {
            return parent::render($request, $exception);
        }
    }
0
votes

Add this code to the app/Exceptions/Handler.php, render method. I think this is clean and simple. Assuming you have custom 500 error page.

public function render($request, Exception $e) {
      if ($this->isHttpException($e)) {
        return $this->toIlluminateResponse($this->renderHttpException($e), $e);
    } elseif (!config('app.debug')) {
        return response()->view('errors.500', [], 500);
    } else {
      // return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
        return response()->view('errors.500', [], 500);
    }
}

use commented line when you need default whoops error page for debugging. use other one for custom 500 error page.

0
votes

In Laravel 8.x & 9.x, You Can Use This Following Method:

In The '.env' file in the root folder of the project, set APP_DEBUG to false

then create a file named 500.blade.php inside ~/resources/views/errors (create the errors folder if you don't have it already).

This will render the custom page on any 500 Server error response.