In Laravel 7 this code works fine. Using renderable
method also works in laravel 8. But I'm not sure how to register it in laravel 8 after creating a CustomException
class.
public function render($request, Exception $exception)
{
if ($exception instanceof ValidationException) {
if ($request->expectsJson()) {
return response('Sorry, validation failed.', 422);
}
}
return parent::render($request, $exception);
}
this worked for me.
The register method
public function register()
{
$this->renderable(function(Exception $e, $request) {
return $this->handleException($request, $e);
});
}
The content of handleException
public function handleException($request, Exception $exception)
{
if($exception instanceof RouteNotFoundException) {
return response('The specified URL cannot be found.', 404);
}
}
I hope you will find it useful.