简体   繁体   中英

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

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.

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

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

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.

    // 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.

in app\\Exceptions\\Handler create the following method:

protected function convertExceptionToResponse(Exception $e)
{
        $e = FlattenException::create($e);
        return response()->view('errors.500', ['exception' => $e], $e->getStatusCode(), $e->getHeaders());
}

it will override the one in the parent class (Illuminate\\Foundation\\Exceptions\\Handler) that displays the whoops page.

In Laravel 5.4, you could override prepareException function in your app\\Exception\\Handler.php :

/**
 * @inheridoc
 */
protected function prepareException(Exception $e)
{
    $exception = parent::prepareException($e);

    if(!config('app.debug')) {
        if(!$exception instanceof HttpException && $this->shouldReport($exception)) {
            $exception = new HttpException(500);
        }
    }

    return $exception;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM