Rendering custom laravel exception is an easy peasy task for laravel. We just need to add our created custom error page on the error view directory. For example if you want to creat a customize error page for HTTPS status code 404, you just neet to create a file with a filename 404.blade.php
and place it in the /resources/views/errors
. Same goes for any other status code you want to customize, as long as you create a filename corresponding to the HTTP status code in the /resources/views/errors
.
The HttpException instance raised by the abort function will be passed to the view as an `$exception` variable. Hence, you can access the exception message and other content of the exception in the blade template error page like so:
Error message : {{ $exception->getMessage() }} Error code : {{ $exception->getCode() }}
But what if we only want to see these custom error pages in production site only? And we do not want to render this custom error pages in our development environment? I myself struggled to do so, I have encoutered a lot of sites sharing different chunk of codes to do this.
What worked for me is updating the `render` function with the following code chunk:
public function render($request, Exception $exception) { if (config('app.debug') && $this->shouldReport($exception)) { return parent::convertExceptionToResponse($exception); } return parent::render($request, $exception); }
What it does is pretty straigth forward. First the config('app.debug')
checks if debug is set to true. You can change this to check the environment instead, if you prefer that. The second $this->shouldReport($exception)
checks if the exception thrown should be reported. Basically all exceptions not listed in the $dontReport
array of the exception hander will be reported. Laravel already set the default exceptions that should not be reported internally. The lists includes the following:
AuthenticationException::class, AuthorizationException::class, HttpException::class, HttpResponseException::class, ModelNotFoundException::class, TokenMismatchException::class, ValidationException::class,
see Laravel code
Also, after further searching on the web while creating this blog entry I have come across this stackoverflow answer, which I think does the job well too(https://stackoverflow.com/a/42629137/9751944).
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 }
Cheers!
Magnificent goߋds from you, man. I’ve understand
your stuff preᴠious to and you are ϳust too excellent.
I really lіke what you have acquireⅾ here, certainly like what you’re saying and the
way in which you say it. You make it enjoyable and you
still care for to keep it wise. I can’t ѡait to read much more from you.
This is really a terrific web site.
Great post. I was checking continuously this blog and I’m impressed!
Extremely helpful information particularly the last part 🙂 I care for such information a lot.
I was looking for this particular info for a long
time. Thank you and best of luck.
It’s an remarkable piece of writing in support
of all the web visitors; they will get advantage from it I am sure.