Replies: 2 comments 1 reply
-
I handle my exceptions the first way. Then I use it like:
and I don't have any problems with writing tests, it actually works pretty well. No need for casts (The outer lamda runs on error) |
Beta Was this translation helpful? Give feedback.
-
One of the caveats of catching exceptions in the presentation layer is the difficulty in handling the variety of exceptions. The presentation layer wouldn't/shouldn't know what kind of exceptions might occur there. By handling exceptions in the data layer, we can decide which exceptions need to be handled and which do not. |
Beta Was this translation helpful? Give feedback.
-
Hello!
I'm trying to figure out where and how exception handling should happen.
I noticed that in your samples you practically do not use a custom class Result. It is only used in NowInAndroid, and only at the presentation level to determine UI state (asResult() method).
My project uses clean architecture and I'm used to handling exceptions on a data layer. I use the following method to call db operations:
It is used in repositories like so:
I see two problems with this approach:
As I already said, in your projects you practically do not use a custom Result class. And so I began to doubt that I was doing everything right. How and where do you handle exceptions? Do I need to do this on a data layer or is it better to move try-catch blocks to a presentation layer?
If we do exception handling at a presentation level (try-catch), then this will get rid of "overloaded" method signatures and make it easier to work with tests, however, I'm not sure if this is the right approach.
I mean that on a data layer we can leave try-catch processing, but not return Result. At a presentation level, use this method for suspends:
And then use it something like this:
Beta Was this translation helpful? Give feedback.
All reactions