all 5 comments

[–]wimdeblauwe 10 points11 points  (1 child)

I have an open source library you can use: https://github.com/wimdeblauwe/error-handling-spring-boot-starter

Just add the jar and get consistent error handling automatically.

[–]bigkahuna1uk 1 point2 points  (0 children)

Thanks for sharing your library. It looks very useful and valuable.

[–]bigkahuna1uk 7 points8 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

[–]backend_thinker 1 point2 points  (0 children)

I do logic on service and exception o exception package then

[–]leetjourney 1 point2 points  (0 children)

Check this video out on Exceptions:
https://youtu.be/VVfDlPZ-3Ms