How do I handle errors for a facade method that makes a lot of scattered background changes?
Imagine I have a service that looks like this:
public interface MyAccountService
{
boolean create( String user );
}
The created method does a couple of changes, namely (for discussion):
- adds a message to the queue
- adds a row to multiple tables
- creates an LDAP account, etc.
I am currently dumping all error messages into a single boolean return value. Now internally, if there is an error, I will log them to support.
eg. typical log of failed user creation
creating an "alistair" account in the following (strict) order:
- add to table Foo: success
- add to table Bar: success
- add to LDAP: failed
- add to queue: success
Thus, the technical support people can decide how to recover the account.
What is the best practice for designing systems so that we can easily track the success / failure of a transaction (and manually repair it)? Returning boolean and swallowing all exceptions good design?
EDIT : By swallowing exceptions, I meant not to throw them to the caller. However, I am logging exceptions and setting them to false / true.
a source to share
In my opinion, you have 2 options: 1. Modify your own exception with a success list so the API client can catch the exception and see what failed, and then decide which action to take. 2. Return an ENUM in which you reflect all the possible outcomes of the result, so again
the API client can decide what action to take.
In any case, you should log all the problems that your method encounters so that it can be traced back ... Eliminating and swallowing the problem is very bad practice.
I like the custom Exception method better as ENUM is more like a C API.
a source to share
I like the approach described in the article presented here , there is a discussion about it here ...
The idea is to consider the kind of exceptions and handle them differently:
One type of exception is contingency, which means that a process has been executed that cannot be successful due to a known issue (an example it uses is checking an account where the account has insufficient funds, or checking has a stop -payment.) These problems must be addressed by a separate mechanism, and the code must wait for them to be managed.
Another type of exception is an error, such as an IOException. the error is usually not something to be or should be expected, and therefore troubleshooting should probably not be part of the normal process.
a source to share
It depends on the need.
If the exception is due to incorrect information sent by the client and you want them to know so that they correct it, you should throw the exception.
If the exception is caused by an internal problem, but still you want the client to know, then throw the exception.
a source to share