Answer the question
In order to leave comments, you need to log in
Is there any generally accepted approach to error handling in the context of the API, where can I look it up?
I came across this interesting article:
https://cerwyn.medium.com/laravel-generalizing-api...
but I'm not at all sure how this approach corresponds to best practices. Can you say something about this?
In general, the question is as follows:
- I want to build some kind of error handling system for the Laravel API through Exceptions, but I don’t want to cycle.
Where can I see some well-known and generally accepted approaches to the issue?
Thank you.
upd .: based on the comments, an example for clarity: You
constantly have to deal with some such things when the model may not be found, and it will either dump the entire stack if debug is enabled, or short and capacious
User::find(100500)->someMethod();
{
"message": "Server Error"
}
Answer the question
In order to leave comments, you need to log in
With the addition, the question became much clearer.
Of course, you can't "fix" it.
Because no handler can replace a programmer.
In the case of generic exceptions, the handler is not able to find out if your base fell off on a request, or just a curve code that did not check for the existence of an object when calling a method.
Therefore, if you need to check the existence of a user and return an error, then this should be done. And as if not yet at the stage of validation.
And here response by hands to do is superfluous. Much easier to throw a custom exception. Which will already be caught by the handler and rendered depending on the type, as shown in the article.
In Laravel, all codes with errors of the author of the article are sent automatically when using validation forms and api resources.
User::find(100500)->someMethod();
You constantly have to deal with some such things, when the model may not be found, and it will either throw out the entire stack if debug is enabled, or short and capacious
Why don't you like this format?
{
'success': false,
'message': 'Ошибка такая-то'
}
Well, perhaps this text is the answer to the stated problem:
https://laraveldaily.com/laravel-api-errors-and-ex...
at least it is clear that the author was asking the same question as me :)
But again, I personally don't have 100% certainty that this is exactly Best Practice.
Maybe someone more knowledgeable can chime in?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question