concept error in category api

This is an excerpt from Manning's book The Design of Web APIs.
You might have noticed that we did not talk about fine-grained data and errors. We will talk about those later in chapter 3 (section 3.3) for fine-grained data and in chapter 5 (section 5.2) for errors. The API goals canvas is only a high-level view; you should not dive too much into details at this stage.
When consumers get one of these HTTP status codes as error feedback, they will know that the problem is on their side if the status code is a
4XXone, but they will have a better idea about the source of the problem. If the error is a404, they will know that the provided URL does not match any existing resource and probably contains an invalid path parameter. If they get a403, they will know that their request is formally valid but has been rejected due to some business rules. If they get a409, they will know their request is in conflict with a previous one. And if they get a400, they will know that their request contains invalid data or is missing a mandatory property.
Let’s say our Banking API is used in a mobile application. Obviously, bluntly showing an
Amount is mandatorymessage to an end user is better than just showing aBad Requestmessage. But wouldn’t it be preferable to highlight the amount field to help the end user fix the problem? How can the mobile application know which value has caused the problem? It might parse the error message string, but that would be pretty dirty. It would be better to provide a way to programmatically identify the property causing the error, as shown in the next listing.Listing 5.2 A detailed error response
{ "source": "amount", "type": "AMOUNT_OVER_SAFE", "message": "Amount exceeds safe to spend" }Along with the error message, we could provide a
sourceproperty that contains the path to the property causing the problem. In this case, its value would beamount. This would enable the program to determine which value is causing the problem in the case of a malformed request. But in the event of a functional error, the mobile application still won’t know the exact type of error. Therefore, we could also add atypeproperty containing some code. Its value for an amount exceeding what is safe for the customer to spend, for example, could beAMOUNT_OVER_SAFE. The corresponding message for the customer could beAmount exceeds safe to spend. Proceeding in this way would enable both humans and programs consuming the API to be able to accurately interpret any errors that arise.
We should therefore return in one shot a list of
errorscontaining the two malformed request errors. Each error can be described as you saw previously in listing 5.3. The same also applies if the request is not malformed but contains multiple functional errors.What happens if there are both types of errors? Figure 5.10 shows what might happen in that case.
Figure 5.10 Handling different types of errors
![]()
In this example, the request contains a valid
sourceaccount but is missing thedestinationaccount and includes anamountexceeding what is safe for the user to spend. When designing single errors, we chose to use400 Bad Requestfor this type of malformed request error and403 Forbiddenfor the functional ones. We could choose to keep these two types of errors separated, and return error feedback to report the missingdestinationfirst, followed by a second message to report the functional error ("Amount exceeds safe to spend"). But does that make sense for consumers? Do they really care about the distinction? Probably not, at least in the specific use case where we provide all the necessary information to determine the kind of error.