Courses

[NEW] React Native Mobile App with Laravel 12 API

Handling 404 API Error

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Modify the Exception handler in bootstrap/app.php to customize API error responses
- Create a user-friendly JSON response for 404 NotFoundHttpException errors

Link to the repository

[Only for premium members]

Now that we have our CRUD operations, we should check that basic functionality works as expected. In this case, we want to ensure we correctly handle 404 errors. Let's look at the default way:

We can do better than this.


Correctly Handling 404 Error

We tried to load a category with the ID of 123 with our first test but got a long message. This was because we have APP_DEBUG=true in our .env file. We can change this to APP_DEBUG=false to see what we would get in a production environment:

It's better since we don't have the stack trace, but it's still not good enough. We are leaking information about our application (specifically, the model name and namespace). We can do better.

So, let's modify our Exception handler to handle this case...

The full lesson is only for Premium Members.
Want to access all 19 text lessons of this course? (91 min read)

You also get:

  • 77 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord