The Exception you send back depends on a few things.
First, did the user send you bad information? Or did your server screw up?
4XX
status codes are for when the user screwed up.
4XX:
If the problem is the user sent a badly formatted request, return 400 Bad Request
.
If the problem is you're using POST to determine if a resource exists (say through search), then a 404 Not Found
would be appropriate if a resource they were trying to access wasn't found.
If the problem is that Inserting this value will cause a problem with an existing value (say they have the exact same name and they shouldn't; or some other validation-esque error: 409 Conflict
is appropriate.
5XX
5XX
status codes are for when your code screwed up. The user did everything right, but you still messed up.
For instance, if your database is down, a 500
or 503
is appropriate.
As an aside, Catching a blanket Exception
is bad. In this case, if you have different validations, you should create exception classes that can be thrown if there is a Validation error; to differentiate it from the user doing everything write but the code going wrong.
In this case, I'd have:
ValidationFailedException
-> Web tier Returns 400
ConflictException
-> Web Tier returns 409
ItemDoesNotExistException
-> Web Tier returns 404
Note that your data layer can should not have a dependency on the web tier or anything internet related. That means that you should have internal Exception classes; throw those, and use those to generate the appropriate HttpException
in your Web Tier.