Contents
Are exceptions part of domain?
If it is a Domain Exception, it means that it is thrown by the domain. Anyway, exceptions thrown by the domain should be handled by the application layer. I have an exception handler decorator for the command bus, that catch any domain exception and translates it into an Application Exception.
What are domain exceptions?
A domain exception is an exception, that is thrown in application’s domain level, which is specific to that particular domain.
How do you handle exceptions in restful web services?
Exception Handling in Spring REST Web Service
- Using ResponseEntity and HttpStatus codes.
- Using @ResponseStatus on the custom exception class.
- Using a custom method to handle error on the controller ( @ExceptionHandler and @ResponseStatus ).
- Return error representation instead of the default HTML error page.
Can a domain model be kept lean with exceptions?
However, from a DDD point of view, the domain model is best kept lean with the use of exceptions in your entity’s behavior methods, or by implementing the Specification and Notification patterns to enforce validation rules.
How are invariants enforced in the domain model layer?
Therefore, invariants enforcement is the responsibility of the domain entities (especially of the aggregate root) and an entity object should not be able to exist without being valid. Invariant rules are simply expressed as contracts, and exceptions or notifications are raised when they are violated.
How to deal with validation errors in domain model?
You can do this by returning a result object instead of exceptions in order to make it easier to deal with the validation errors. Using field validation with data annotations, for example, you do not duplicate the validation definition.
When do domain entities have to be valid?
Domain entities should always be valid entities. There are a certain number of invariants for an object that should always be true. For example, an order item object always has to have a quantity that must be a positive integer, plus an article name and price.