Whether it is your own Exception class or something from inside the framework, you can check for the type and return your preferred response/view:
// app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
if ($exception instanceof \Some\Exception::class) {
return view(
'error.view', // your custom view
['message' => $exception->getMessage()], // any data you want to send to the view
$e->getCode() // exception code
);
}
return parent::render($request, $exception);
}
If you find that you are checking for a lot of custom exceptions inside Handler.php, then you could choose to define a render method inside your own custom class, and return the response from there:
// app/Exceptions/YourCustomException.php
public function render($request, Exception $exception)
{
return view(
'error.view', // your custom view
['message' => $exception->getMessage()], // any data you want to send to the view
$e->getCode() // exception code
);
}
https://laravel.com/docs/5.6/errors#render-method