Error handling has always been something quite difficult to grasp in software design and still is.
Exceptions are now widespread in languages, and it helps a lot to manage corner case where something fails badly.
But should we use Exceptions to manage business errors ?
The business errors
What do we call business errors actually ?
What if an invariance rule is broken ?
The situation should never happen : There is a bug. A bug is not a business error, correct it and deploy.
The situation can happens sometimes : This is not an invariant, but a rare state. It should be handled as any other state change.
What if we receive an invalid command ?
The command data is meaningless : There’s a bug, you should always validate that command data is not just garbage.
The command leads to an invalid state : The user nonetheless requested to perform the command.
In this case the event will be ‘the request was rejected’. The event can be handled by sending an email back to the customer, or a support request can be started so that the support can call the customer and manage the problem. All this is part of the business process anyway.
Corner cases create business opportunities
I can often see discussions around account validation for credit, to make the transaction fail when your account goes below zero.
But it’s not what’s happening in real life. Transaction is accepted, then the bank charges you because your account is in the red zone.
I’m currently working in the hotel business. When a booking is received and there’s no room left, should I reject the booking ? Another client can cancel soon, or I can move the customer to another hotel nearby, but just saying ‘there’s no room left’ is not a good business answer ! Overbooking management has even become a strategic practice in the business.
To fully manage your customers you should embrace the whole business lifecycle in your system. This includes support and corner case management. Part of it will be done by hand, other part automatically, but you should not just report an exception is a trace log.
These critical situations are usually the one in which you customer needs you more than in any other case, you should design your fault handling strategy with care and make it a full concern of you business.