-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why not to put exceptions from the data layer into domain layer? #141
Comments
Because those exceptions belong there. An exception like, for example, "CacheKeyMissingException" should reside in the same layer that it is used, ie: data layer. The exception "ButtonTextEmptyException" is obviously related to ui, so it belongs in the presentation layer. Keep exceptions where they belong. Exceptions are not part of any "logic", nor should be used as a control flow. Also, because the way this project is set up, you need one "entry point" to start the app and know about all the code, and in this case, this is the presentation layer. I suspect your question pops up because "Exception Handling" is more of a cross-cutting concern, as it something that touches all the layers, but is not exclusively part of one layer. So each layer has specific tasks related to "Exception Handling".
So, let's imagine an app where i can order a puppy with the press of a button. I press the button, but i'm on a train driving through a tunnel. I'll press the button, Presentation notifies Business of said press. Business logs "Trikke wants a puppy" via Data in a file. Business call Data to make a request to the API and order a puppy. I'm in a tunnel, so the network request fails miserably after a timeout. Data catches this and notifies Business with a general "ApiNotAvailableException". Business notices and removes the log "Trikke wants a puppy". Business notifies Presentation that an unrecoverable error has happened. Presentation shows me a nice messages stating "We couldn't order you a puppy because the app is experiencing network troubles". |
@Trikke, In your example the "ApiNotAvailableException" will be converted to "We couldn't order you a puppy because the app is experiencing network troubles" message in the presentation layer? (in that case "ApiNotAvailableException" is a property of data layer, but the presentation layer has to know about that exception). Is it ok in terms of layered architecture? |
Please read up on my other comments on another question about layering. It's a bit of a hard concept, because layering in this project's setup != layers in Clean Architecture. Parts of both the data and presentation layer of this project's setup exist on the same layer in Clean Architecture, so there are no dependency issues. |
@Trikke, could you help me, please, to place the things in the right way in some specific scenario? :-)
|
Like i said, the "Exception Handling" part of the data layer consist of shielding the app from any problems of external sources (SQL, File storage, Network, ...) which all each have their own set of exceptions and/or problems. |
@Trikke, thank you for your help! |
Why not to put exceptions from the data layer into domain layer and make them as a part of business logic? In that case the presentation layer will be completely separated from the data layer: now the ErrorMessageFactory class uses NetworkConnectionException and UserNotFoundException from the data layer.
The text was updated successfully, but these errors were encountered: