Trying to come up with a strategy for handling errors and exceptions, we came across some interesting discussions and/or Holy Wars.
I’ll try to elaborate a bit on my thoughts and the strategy that was decided for the plugin.
First of all the purpose of the Glickr plugin (or any plugin the implements some interface I guess) is to make the translation of the native language constructs and classes (objects) to the particular API and vice versa.
Ideally, the user of the plugin should not be bothered with the specification or workings of the Flickr API at all. Furthermore, the user of the plugin will actually be a programmer creating an application that will or will not have a humanoid working with it. How this application deals with errors and exceptions should not be up to me (the developer of the API) but up to my user (the programmer of the application). So I should keep as much under wraps as possible, but not too much. Still with me?
To make the rest of this post easier to read, let’s agree on the 4 different actors layered on top of each other in our scenario;
According to a dictionary, a Recoverable Error is “‘an error that does not prevent program execution’” which is interpreted by me as “’not meeting the business rules’”, which are in this case, some rules of the Flickr API, like ‘at least 1 result should match your search criteria’. Real world business rules of an application are beyond the scope of the plugin.
A Human or Programmer searches for Obi-Wan Kenobi in the Star Trek database.
Result? Not found, but a recoverable error (search for another person or in another database).
An Error but not an Exception.
Humans can make Errors, but it is up to the Programmer to decide how to interact with the Human about this unfortunate situation (if there is a Human involved at all, think good old-fashioned batch programs).
We still agree it’s an Recoverable Error not an Exception, don’t we?
The API could report back to Me that the “Service is temporary not available”.
While this does give Me a hint that this error could be recoverable, it is very hard for Me to deal with with the underlying problem.
The (help-desk of the) Programmer might figure out when Flickr finishes their backup of 7 billion images or if there was a hostile takeover by Buy N Large, but that’s as close to recovery we could get.
The API will respond this Recoverable Error every now and then, but neither Me nor a Human could actually deal with the possible recovery (such as: when and/or how often to retry).
We’ve dealt with the Human and the API so far, they both are allowed to make errors, and we can not blame ’em for it. What’s left to deal with are errors that are introduced by one of the other two actors. As the developer of the plugin I do not know who the Programmer will be, so I need to provide her/him/they with the information needed to blame Me for the error that should be solved in the plugin and will friendly inform her of errors that she can solve herself (syntax problems mostly, specially since the interface will probably not be 100% type-safe and will use plenty of late-binding).
To prevent error handling code from messin’ up the undoubtedly beautiful code produced by the Programmer I will be throwing Groovy Exceptions where applicable. Here’s what I decided to implement for the different types of errors we’ve just elaborated on;
This should enable the programmer to write code that just checks for business logic (is there an object returned or a NULL value) and catch FlickrServiceException (all three flavours will inherit from it) at a more sensible place in the application. Last but not least, while the thrown Exception will include detailed technical information about the actual source of the problem, one should never, and I do mean NEVER, bother a normal human being with this cryptical programming stuff. Promised?