Domain Driven Challenges: How to handle exceptions
Welcome to my series Domain Driven Challenges, where we dive into Domain Driven Design topics that often are not focused on enough and I have seen implemented wrong while being a Tech Lead. The examples are written in Java but can be applied in most languages.
One of the core concepts of Domain Driven Design (DDD from now on) is the Ubiquitous Language. We understand what the function or class is doing based on the naming, and even the business domain users should be able to follow along.
Many times we read a lot of great examples on how to implement a lot of different things:
Service Layer pattern
Repository pattern
Using interfaces and infrastructure
Dependency inversion
However, a lot of these topics are mostly covered in the positive or happy flow, but what if we also want to properly handle edge cases or scenario’s that should trigger an error?
Enter Exceptions
Exceptions allow us to abort functions and code when certain conditions are met and let higher level code know that something went wrong (by catching the exception). To explain this concept, let’s setup an example:
We are building a payment service
The user pays through their balance
If the balance is not high enough, we can not process the payment
The payment is handled by an external API service
Because not every API we integrate with is perfect, we add some additional complexity:
The external API service does not check balance when handling the payment
The external API does have an endpoint to retrieve balance
The external API has an unstable uptime.
Happy flow
Before we can explore error handling, we should setup a simple function to handle the happy flow:
0 Comments