0

I am working on REST project using jersey. On success I am returning 200 code along with the json response for particular request. I know there are may different classifications of error codes, like server error which start with 500, client error which start with 400 etc. My question is suppose we are subtracting some value in database for example count, for example count in database is 5 and request comes to subtract 3, it is valid and i will send request but my business rule states that count cannot be less than zero, so if request comes 6, i cannot subtract that , so in that case should i actually send status code as 200 and send error information is json respose {"errorCode" : "1","errorMessage":""} so i should send different HTTP status code like 5## that there is server problem or 4## saying bad request.

Can anyone please suggest me good (in the sense which is restful and follow all standars) REST project on github which I can refer.

3 Answers 3

1

If any error occurs during request processing you should never send a 2XX status code. Why? Because 2XX indicates successful processing which in this particular situation did not happen.

When you send a value to be subtracted from another value in DB and the assumption is that the result can't be lower than 0 you should reply with 409 Conflict HTTP status code and clarification in a response body stating e.g.:

The request can't be completed since the result value will be lower than 0.

It would be 400 Bad Request if null e.g. is sent instead of a number.

Sign up to request clarification or add additional context in comments.

1 Comment

so it means if user sends invalid request, like negative count ,etc it should be bad request ,but if I do not have required count than it is not fault of user and so 409 should be send ,200 should only be send when whatever user intends of achieving through request happens as per contract !
1

In the particular use case, you mentioned, you should return 400 as status code (bad request). errorCode in the json is your business domain, so you can use what you want ( Also, take a look at JSend standard for sending the http "error status" in the response https://labs.omniti.com/labs/jsend )

Comments

0

You're exactly right: you should send an HTTP "400" for a client error, a "500" for a server error, etc; along with a specific error message.

You may or may not want more granular HTTP status codes (for example, "403" for client authentication failed, otherwise "400" for other client-related errors).

Are are two good lists you can use for guidance:

NOTE:

There is no "standard" per se. The two links I gave above are useful "examples". The "official" IETF RFC for HTTP 1.1 Error codes is RFC 2616:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.