Laravel/Lumen route exception

If you want to create a custom error on your app routes just follow this steps:

Find the file app/Exceptions/Handler.php

Add use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; at the top.

In render function add this code

if ($e instanceof NotFoundHttpException) {
return response()->json(['error' => 'Route not found'], 404);
}

Now, if you try to access any inexistent route will return the error:

{
"error": "Route not found"
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: