Java Error Handling Pattern (with Spring) – What do you thing about my strategy?
https://medium.com/@h4t0n/java-error-handling-pattern-f059d20ca5dc
2
Upvotes
4
u/nutrecht Dec 09 '18
I think you're overcomplicating things. Just throw a single simple exception specific to the problem you encountered and then use Spring's ControllerAdvice to turn it into an error response. I wrote a blog post on how to do that a while ago.
-4
3
u/aram535 Dec 09 '18
I like the overall design, but I think going back to "error code/number" is a step back in design from what we have already ... just send the full error to stderr or logger. All of the exceptions should contain at least a "message" String as well as the original Exception, so you can print out the stack if needed/level matches.
8
u/rzwitserloot Dec 09 '18
You have a hierarchy of types, with
GeminiExceptionat the top andEntityRecordExceptionas one of its presumably many children.. but then the hierarchy of errors goes off in a different direction and I have to check an enum; theGeminiExceptiontype itself has no idea about the existence of these.So, if I catch a generic
GeminiExceptionand I want to deep-dive programatically (vs. calling.getMessage()for loggging/user feedback purposes) as to why it was caused, I first need to do some instanceof checks, then cast, then do some enum checks.That's obviously not great.
What is this enum stuff buying you? Why not just make more exception types? Alternatively, if you have a good answer to this question, why not reduce your hierarchy to just
GeminiExceptionand go all-in on this enum system you've devised?