Let's assume we have a payment service that consists of multiple levels to confirm a payment.
The mentioned process is like this :
- first we try to confirm the payment with the main payment provider, if everything is alright we continue if not we exit here.
- in this step we try to fetch the payment information from our own db, if no payment is founded we do an immediate exit here too.
- if the payment depends on another payment, we try to confirm the dependent payment, if the second payment confirmation is okay we continue, if not we have to cancel the first payment and error out to the user. Of course the main payment cancellation can throw an error too then we need to log the error and notify the user to call customer service for example.
- in this step we try to provide the user for the requested service. If all goes well then we exit the process, if not then we have to cancel the main payment and also cancel any existing dependent payments too, which could result to cancellation failures and we should handle them appropriately.
Writing this process results to nested branches and complex try-catch blocks for error handling.
What would be the best pattern for handling these scenarios that could lead to multiple errors during runtime and we have to act on each error scenario and a generic answer is not gonna cut it.