What approach is suitable for handling and returning errors (not exceptional and exceptional) in objects managed by domain design and aggregate roots?

I am trying to find a good article / examples of how DDD objects handle errors (and what will be considered exceptional errors and what will not), and how they pass them to the calling application layer (which usually wraps operations in a transaction that should be discarded back).

Currently, I am going to consider all errors that could exclude an aggregate transaction (for example, checks). That way, I can cancel the transaction in the catch block. For instance:

SomeApplicationService:

// start transaction here // ... try { $user = $userRepository->userOfId($id); $user->doSomething(); $user->doSomethingElse(); // <-- imagine an error thrown here $userRepository->save($user); } catch (CustomFriendlyForUIException $e) { // Custom Friendly for UI error // Rollback transaction and add error/message to UI payload DTO // ... } catch (AnotherCustomException $e) { // Not friendly to UI, so use general error message for UI // Rollback transaction and add error/message to UI payload DTO // ... } catch (Exception $e) { // Catch all other exceptions, use general error message for UI // Rollback transaction and add error/message to UI payload DTO // ... } // end transaction 

Is this the right approach, or am I missing something?

+6
source share

All Articles