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.
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
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 ?
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 ?
Thank you for this course
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.
While it's individual, for me it always comes down to:
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
Thanks for the detailed answer! Exceptions are also important, for example, in the processing of requests to payment systems.
Definitely! But I didn't mention it because that feels self-explanatory :)
great tutorial!
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 ?
I would not cover retrieving operations with it, unless it's crucial. But I would cover majority of insert/update ones!
alright thanks
Useful course. Thanks
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 ?
Can you write an error message you get in this case?
App\Services\Api\V1\UserService::getUserById(): Argument #1 ($id) must be of type int, string given,
In this case, the error is from types/php. So better solution would be to add regex validation on route params
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
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.