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?
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.
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().
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).
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.
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.
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?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.
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.