R
R
Romi2021-10-08 08:44:24
API
Romi, 2021-10-08 08:44:24

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"
}


if off. but I want something in between that can be given to the front, understandable, but without too much information.

I used to usually write all sorts of checks right in the body of the function, or optional () used, or something else in the same vein. Response hands did for each case.

But maybe this can somehow be solved through customization of Exceptions? How to automate more or less?

The guides I found on the net are pretty sparse. At least in the context of my understanding of this area.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
Ipatiev, 2021-10-08
@romicohen

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.

J
jazzus, 2021-10-08
@jazzus

In Laravel, all codes with errors of the author of the article are sent automatically when using validation forms and api resources.

T
ThunderCat, 2021-10-08
@ThunderCat

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

In theory, just wrap a tri-catch and return an error, BUT! In this case, since you assume "the presence of a user or an error", it is more correct to use not find but get, then an exception about a missing object will fall out.

N
Nadim Zakirov, 2021-10-11
@zkrvndm

Why don't you like this format?

{
   'success': false,
   'message': 'Ошибка такая-то'
}

R
Romi, 2021-10-08
@romicohen

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 question

Ask a Question

731 491 924 answers to any question