Search code examples
pythonhttphttp-status-codesvalueerror

What is the http status code when Api raises ValueError?


I am raising Value Error in my API because the input parameter of a particular function is not valid like below

  1. Password doesn't match
  2. User doesn't exist in db or the value is negative

Client provided valid argument as per the API norms so I think Client side error is not the case(400 series code).

So whether I have to return status code as 200 because that request is totally processed or there should be a http status code for this.


Solution

  • As there are various invalid types, you should use the most appropriate HTTP status code for each different situation, case by case.

    For Password doesn't match, I think 403 Forbidden is the best choice.

    For User doesn't exist in db, 204 No Content is the best one.

    For value is negative, it depends on why value is negative.