I searched the internet and still did not find a mature enough answer to a problem.
I have an Ajax request and taking errors thrown by the application as a possible connection crash, for example, I do not want to throw an exception if I have a business error.
For example, the user enters an invalid address in the email field. I want to throw an error, but not as request error so that the error function of the jQuery ajax object will deal . I do not want something like HTTP errors of the 400 and 500 ranges.
I want to return status 200, because the request was successful, but somehow identify that there was a business error.
I see a lot of people returning a JSON object with a boolean attribute of success / failure and if it fails this same object has a another JSONArray object with the error messages.
Is this the best way to really drive when there is a business error, not the infrastructure or the application itself?
No , this is not the best way. You should return a 400 error. HTTP is an application protocol and so if you return code 2xx the client can assume that your request was accepted regardless of any response body content.
From RESTful Web Services Cookbook :
A common mistake that some web services make is to return a status code that reflects success but include a message in the response body that describes an error condition. Doing so prevents HTTP-based applications from detecting errors. For example, the cache ( of the browser, or a reverse proxy for example ) will store this response as a successful response and serve it to the next customers even when the clients were able to make a request correct.
Use 4xx codes when the client is able to correct your request and resend correctly, use 5xx codes when it is a server error, and regardless of a fix on the request, it does not make sense to resend it.
RESPONSE BASED ON link