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? (30 h 41 min)

You also get:

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

Already a member? Login here

Comments & Discussion

ZA
Zain Amin ✓ Link copied!

Thank you for this course

S
Slon ✓ Link copied!

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.

M
Modestas ✓ Link copied!

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

S
Slon ✓ Link copied!

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

M
Modestas ✓ Link copied!

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

LV
Luis Viera ✓ Link copied!

great tutorial!

F
F_r ✓ Link copied!

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 ?

M
Modestas ✓ Link copied!

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

F
F_r ✓ Link copied!

alright thanks

LN
Loganathan Natarajan ✓ Link copied!

Useful course. Thanks

K
kalDeveloper ✓ Link copied!

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 ?

M
Modestas ✓ Link copied!

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

K
kalDeveloper ✓ Link copied!

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

M
Modestas ✓ Link copied!

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

K
kalDeveloper ✓ Link copied!

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

M
Modestas ✓ Link copied!

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.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.