Skip to main content

Handling Exceptions in Laravel API

Premium
4 min read

The Full Lesson is Only for Premium Members

Want to access all of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Zain Amin avatar

Thank you for this course

Slon avatar

Thanks for the course, but I missed the theory about the reasonable use of exceptions. Now it looks to me that at least 50% of almost any code can be surrounded by try/catch constructs and create dozens or even hundreds of exception classes. I understand that this is all very individual, but maybe you can tell a few words from personal experience how you determine when to use exceptions.

Modestas avatar

While it's individual, for me it always comes down to:

What will happen if there's an issue here? Will the flow break? Do I handle it correctly?

And once that is clear - you either add an exception or improve validation of the data. For example, imports are a great place to add exceptions, to prevent complete failure. Same goes for any public facing API - exceptions are a really nice way to handle situations where someone misused something (sent wrong input or just doesn't have the correct data inside). In any case, I wouldn't go too deep to handle everything, as some things will be handled by framework in a nice way (yet, it is important to have a bug tracker with it!). If it isn't handled - I'd see importance of this working and returning an output with a friendly message

👍 3
Slon avatar

Thanks for the detailed answer! Exceptions are also important, for example, in the processing of requests to payment systems.

Modestas avatar

Definitely! But I didn't mention it because that feels self-explanatory :)

Luis Viera avatar

great tutorial!

F_r avatar

Helloo thanks for this course. Having a question. Base on this above code example should I base all my database relative operation, like Eloquent retrieving and fetching (as you did for user) inside a try-catch ?

I no, when do your really recommand to use it ?

Modestas avatar

I would not cover retrieving operations with it, unless it's crucial. But I would cover majority of insert/update ones!

Loganathan Natarajan avatar
Loganathan Natarajan

Useful course. Thanks

kalDeveloper avatar

Hi , what if user type like this http://api.test.com/api/v1/users/3dd ? how to catch this error? I tried adding multiple exceptions but even Exception class does not catch it. can somebody help ?

Modestas avatar

Can you write an error message you get in this case?

kalDeveloper avatar

App\Services\Api\V1\UserService::getUserById(): Argument #1 ($id) must be of type int, string given,

Modestas avatar

In this case, the error is from types/php. So better solution would be to add regex validation on route params

kalDeveloper avatar

this what in the api.php

Route::apiresource('/users', UserController::class);

in this case should i have to specifiy each route and add the regex to one route or is there any other way

thanks

Modestas avatar

Sadly - yea. I don't remember seeing a better way to do this (unless running a middleware that would check if it's okay). But that complicates things.