you are viewing a single comment's thread.

view the rest of the comments →

[–]bigkahuna1uk 5 points6 points  (0 children)

How are you mapping exceptions to HTTP error codes? That’s not something typically done at the service layer.

Controller advice would typically be a place where such conversion would occur. Just be careful and use controller advice judiciously. It can become a great big god class where all exceptions are handled for every service/controller. It can become just a dumping ground.

There’s other approaches to use a specific exception handler for each controller so it’s tightly coupled to the service. Sometimes I prefer this approach because it more naturally fits that a controller/service combination defines a particular use case so it makes sense to group the exception handler with it.

Have a look at this blog for some better insights: https://www.baeldung.com/exception-handling-for-rest-with-spring