At some point we need to provide our users with something better than the standard Whoops! Something went wrong… page. Here’s how:

The following snippet works as of Laravel 5.4.17.

Navigate to and open app/Exceptions/Handler.php

We’re interested in the render() function, that should look like the following:

<?php

/**
* Render an exception into an HTTP response.
*
* @param  \Illuminate\Http\Request  $request
* @param  \Exception  $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
    return parent::render($request, $exception);
}

To get to the heart of our exception we’ll add the first little snippet:

$statusCode = FlattenException::create($exception)->getStatusCode();

We also need to check if this is a ValidationException, as we don’t want to mess with how our forms are treated.

$isValidationException = $exception instanceof ValidationException

Then finally we perform our check:

<?php
if ($statusCode === 500 && !$isValidationException && app()->environment() == 'production') {
    return response()->view('errors.500', [], 500);
} else {
    return parent::render($request, $exception);
}

Your render function should now look like this:


<?php
/**
* Render an exception into an HTTP response.
*
* @param  \Illuminate\Http\Request  $request
* @param  \Exception  $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
    $statusCode = FlattenException::create($exception)->getStatusCode();
    $isValidationException = $exception instanceof ValidationException;
    if ($statusCode === 500 && !$isValidationException && app()->environment() == 'production') {
        return response()->view('errors.500', [], 500);
    } else {
        return parent::render($request, $exception);
    }
}

Happy customizing!