DEV Community

Discussion on: Handle Spring Exceptions Like A Pro

Collapse
 
mi_native_nutt profile image
Mark

This is a way to do it. The best? Probably not. First spring typically handles most errors in a generic way that is good enough for almost all clients. Most clients are only going to care about failure or security or success. If not the actual best way to do it is to throw a custom exception and annotate that exception so it will throw the desired HTTP code. That way you have a custom exception which is the right thing to do and then handle them in a specific way versus a somewhat less generic way via the controller advice

Collapse
 
alwynschoeman profile image
Alwyn Schoeman

I agree for internal facing applications. For external you don't want the stacktrace to be sent to the client since it allows would be hackers to gain insight into the inner workings of your application.

Collapse
 
mi_native_nutt profile image
Mark

See that's the thing about springboat there's always a way to handle that via configuration. There is a one line configuration to turn that off and you can do it by environment with profiles

Collapse
 
jarjanazy profile image
Abdulcelil Cercenazi

Thanks for the feedback :) yeah you are right, however I've found that controller advice can also be like a final fishnet for any unexpected events.

Collapse
 
mi_native_nutt profile image
Mark

Yeah I understand what you're saying but I've got about 40 spring boot applications and I've never needed it. The default spring boot mechanism handles the majority of unexpected events.

Thread Thread
 
jarjanazy profile image
Abdulcelil Cercenazi

I have used it :D so it really depends on your work style

Thread Thread
 
mi_native_nutt profile image
Mark

Well it's not really work style it's really about if you're using spring boot techniques or Reinventing the wheel. But I mean if that's what you want to do go for it LOL

Collapse
 
gorynych profile image
Stepan Mozyra

Setting HTTP code for you own exceptions - is the best way to high coupled monolith with mixed everything together. "The best? Probably not." )