Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mpmurph's avatar

Handler.php - trying to create a default error page but am accidentally catching all form validation errors too

Hello all -

I am using Laravel 5.4 and am trying to customize exception/error handling a bit.

Right now, in the production environment, I explicitly catch TokenMismatchException and re-route the user back to login with an error message. Additionally, I catch the HTTP status codes 401, 403, 404 and 503, each of which has a custom error page attached to it.

What I would like to do with all other errors in production is go to a default/generic error page. However, when I try to implement this, it's also catching form validation errors and, instead of returning the user to the page where they made the error with the appropriate validation message, it redirects them to the default error page.

If I use the default return statement (as follows), validation on forms proceeds as expected, but if there is any other type of exception (other than a TokenMismatchException or an HTTP exception matching one of the above status codes), Laravel's "Whoops!" page is displayed (but without any details as to what the error is).

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

Ideally, I would like to retain the expected validation behavior (i.e. returning the user to the page where the error was made with a message explaining what they did wrong), while capturing any other exceptions/errors and redirecting them to the default error page (instead of the Whoops page). As a bonus, I would like to be able to trigger an e-mail reporting the error to the developer (me) at the same time.

Exception handling is still pretty new territory for me so any thoughts/recommendations as to how I might be able to accomplish this would be much appreciated. Thank you!

0 likes
1 reply
mpmurph's avatar
mpmurph
OP
Best Answer
Level 1

SOLUTION: was able to catch validation errors in Handler's render() method with the following test and retain the expected behavior:

$exception instanceof ValidationException

All other errors either have a custom error page or are redirected to a default error page. As for error notification, I opted to implement a third party exception reporting service.

Please or to participate in this conversation.