Say I have an REST API for creating users in an application. The request goes to a controller, which marshals the request data into a domain object, and passes it to a service to create a user.
Now, there are certain uniqueness constraints on a user. If the user service receives a request to create a duplicate user, is it appropriate to throw an Exception at this point, e.g. DuplicateUserException
, or is there a better way of handling something that is a predictable error case, i.e. not an exceptional circumstance)?
The goal here would be to provide as much detail to the API client as possible, to allow them to correct their mistake.