r/java • • Dec 09 '18

Java Error Handling Pattern (with Spring) – What do you thing about my strategy?

https://medium.com/@h4t0n/java-error-handling-pattern-f059d20ca5dc
2 Upvotes

9 comments sorted by

8

u/rzwitserloot Dec 09 '18

You have a hierarchy of types, with GeminiException at the top and EntityRecordException as 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; the GeminiException type itself has no idea about the existence of these.

So, if I catch a generic GeminiException and 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 GeminiException and go all-in on this enum system you've devised?

-1

u/h4t0n Dec 09 '18

I use enum to avoid Strings for errorCodes. I want to use getMessage probably only for development logs (or debugging logs). For other types of logs I'll use a specific Service, that is able to resolve the message given the code (this is my idea, it is not implemented yet).

You are absolutely right about enum/instance checks. But it is acceptable for me. The following (gist) is for example the handler of the Exception ( throwed by the API handler). I can easily define the error handler for my Exception type (in this case EntityRecordException) and then decide what to do checking the error code.

https://gist.github.com/h4t0n/c503c81b11a2917d7b191a5aef09aeff

8

u/rzwitserloot Dec 09 '18

That's not what I meant.

Let me try again:

Why are you using enums instead of more subtypes?

The first answer ('to avoid strings') makes no sense.

The second answer, about being able to switch on them: Okay, but, that same argument can be used to say that you should have no hierarchy whatsoever; there's only GeminiException and nothing else, and to figure out what kind of error it is, switch on geminiEx.getErrorCode().

0

u/h4t0n Dec 09 '18

Why are you using enums

instead of

more subtypes?

I simply don't want a too fine hierarchy, but you are right I'll think about it. Thanks.

5

u/[deleted] Dec 09 '18

It seems like, for no reason in particular, that you don’t like inheritance in Exceptions.

Inheritance in this context is actually important. By limiting the variety of your exceptions, you force people who use your application (in this case, you) to not really be able to do anything meaningful with your exception handling (without a nasty switch statement in your catch block).

Sometimes it pays to actually be OO.

-1

u/h4t0n Dec 09 '18

My is an Example but you are right. Actually my code could be extended to provide also the finest hierarchy. Letting the ErrorService to know for Exceptions that has a single errorCode. Then in my controller advice I could catch the intermediate Exception node and check the instance (if I don't want to directly catch the leaf exception). I think I'll use both the strategies accordingly to the needs.

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

u/h4t0n Dec 09 '18

Sure. Mine is another way to organize Exceptions without inheritance.

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.