Search code examples
androidperformancekotlinandroid-mvvmandroid-architecture

The better way of Error Handling In Android


Let's say I have this architecture in my app Architecture

My question is when is the best time to handle the error?

  1. Should I let the network source and cache source throw an error, and we handle all the possible error in the repository layer
  2. Should I handle the error in the framework-specific, and return a sealed class that present is the network call error or success

Solution

  • Depends on the use case

    • If user is expecting data, then you need to send the errors all the way to your UI to notify user to make sure user won't just see a blank screen or experience unexpected UI. You can sealed classes in Kotlin for this case.
    • If you are fetching something which user is not aware of, like syncing data from remote to Local cache in background, In cases like this user is not aware/ do not care about errors, your app should handle. Ideal way is to check in handle error in repository, if something fails, retrying it there or fetching data from alternate source etc.
    • Third case would be handling at multiple levels, say you are fetching data from network, it failed, you can handle retry logic in repository level and after you exhaust your retry attempts but still it is failing then you need to acknowledge user as he might be expecting some thing to show up.