Home » Php » how to show 500 internal Server error page in laravel 5.2?

how to show 500 internal Server error page in laravel 5.2?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I want to show the page 500 internal server error Page. when user had syntax error mistake in project can anyone help me? if i do some mistake in syntax i want to show that particular blade.

Answers:

You need to create handler to catching FatalErrorExceptions in your handler like below code:

Handler
In app/Exceptions/Handler.php

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

    // 404 page when a model is not found
    if ($e instanceof ModelNotFoundException) {
        return response()->view('errors.404', [], 404);
    }

    // custom error message
    if ($e instanceof \ErrorException) {
        return response()->view('errors.500', [], 500);
    } else {
        return parent::render($request, $e);
    }

    return parent::render($request, $e);
}

View
See resources/views/errors/500.blade.php. If not exist then create it.

You can get more detailed OR other ways from Laravel 5 custom error view for 500

Questions:
Answers:

In your resources/views/errors folder create a file named 500.blade.php.

Laravel makes it easy to display custom error pages for various HTTP
status codes. For example, if you wish to customize the error page for
500 HTTP status codes, create a resources/views/errors/500.blade.php. This file will be served on all 500 errors generated by your application.

The problem is that Laravel will only do this automatic rendering of error pages for exceptions that are instances of HttpException. Unfortunately when your server throws an error (method does not exist, variable undefined, etc) it actually throws a FatalErrorException. As such, it is uncaught, and trickles down to the SymfonyDisplayer() which either gives you the trace (debug true) or ugly one-liner ‘Whoops, looks like something went wrong’ (debug false).

To solve this you have add this to your render method to app/Exceptions/Handler

# /app/Exceptions/Handler.php

# use Symfony\Component\Debug\Exception\FlattenException;

# public function render($request, Exception $e)

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

if ($statusCode === 404 or $statusCode === 500) {
    return response()->view('errors.' . $statusCode, [], $statusCode);
}

Docs

Questions:
Answers:

My solution is simple, just replace your render() method in Exceptions\Handler.php file with:

/**
 * 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)
{
    if ($request->expectsJson()) {
        return $this->renderJson($request, $exception);
    }

    if ($this->shouldReport($exception) && app()->environment('production')) {
        $exception = new HttpException(500, $exception->getMessage(), $exception);
    }

    return parent::render($request, $exception);
}

It will show 500 page if app in production environment. You will need to have 500.blade.php view in your resources/views/errors folder.

Questions:
Answers:
    // app/Exceptions/Handler.php

    protected function prepareResponse($request, Exception $e)
    {
        if($this->isHttpException($e) === false && config('app.debug') === false)     {
            $e = new HttpException(500);
        }

        return parent::prepareResponse($request, $e);
    }

Like @Amit said

The problem is that Laravel will only do this automatic rendering of
error pages for exceptions that are instances of HttpException.

So my solution is to replace whatever exception that is not HttpException by a HttpException.