Islam
Islam

Reputation: 17

Why render function in ExceptionHandler in laravel dosen't execute?

I want to show the page 500 internal server error Page but instead of showing report or render it just displays the typical Laravel exception view with the error message .

public function report(Exception $exception)
{
    parent::report($exception);
}

/**
 * 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)
{

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

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

Upvotes: 0

Views: 1328

Answers (4)

Harshad Kanani
Harshad Kanani

Reputation: 56

Here's how you can modify your render() method to show a custom error page for 500 errors and let Laravel handle the rest

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

public function render($request, Exception $exception)
{
    if ($exception instanceof ModelNotFoundException) {
        return response()->view('errors.404', [], 404);
    } elseif ($exception instanceof NotFoundHttpException) {
        return response()->view('errors.404', [], 404);
    } elseif ($exception instanceof HttpException && $exception->getStatusCode() == 500) {
        return response()->view('errors.500', [], 500);
    }

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

Make sure you have error view files created for both errors.404 and errors.500 in your resources/views/errors directory.

Upvotes: 0

dmaon
dmaon

Reputation: 68

since this question is relatively new I presume you are suing Laravel version 7 or 8. this is because of your render function:

function render($request, Exception $exception) 

in the original ExceptionHandler class that your Handler class extends from that, the render function is like the below:

public function render($request, Throwable $e);

the parent class needs the second parameter to be Throwable type, but you use the Exception type in your child class.

Upvotes: 1

PtrTon
PtrTon

Reputation: 3835

Could you check your bootstrap/app.php file to see if the exception handler is bound correctly? The default configuration would be like this.

A while back I wrote a post about implementing a custom exception handler in Larvel, it might contain some useful information for your issue.

Upvotes: 0

Joe
Joe

Reputation: 4728

If you're seeing the woops message on 500 errors instead of a 500 error page, it is because the app is in debug mode.

In your .env file edit the following line

APP_DEBUG=true

to be

APP_DEBUG=false

Upvotes: 1

Related Questions