brunocleite / spring-boot-exception-handling

Final source code accompanying the article on the Toptal Blog.
https://www.toptal.com/java/spring-boot-rest-api-error-handling
MIT License
493 stars 288 forks source link

Custom HTTP status codes #4

Open davidvasquezr opened 4 years ago

davidvasquezr commented 4 years ago

Hi @brunocleite Thank you for your explanation, it is really useful.

How to use this with custom HTTP status codes?. I need an error code that is not in the HttpStatus class, something like "UserDisabled" or "EmailAlreadyExists" etc.

eheinen commented 4 years ago

Hi @davidvasquezr, I was just checking this project right now and saw your question. I was thinking about your question and are you sure you want a new HTTP status code? for me those "error codes" you provided are more detailed reason than status code. Personally, I would map it as status code 409 as conflict and set a message telling the reason. Or if you really want to categorize it, you could create a new attribute in ApiError called category or code and then you could just set there. And in the response it would be something like

409 Conflict

{
   "code": "user-disabled",
   "message": "The use is already disabled..."
}

WDYT?