How to Format All Possible Errors From API Calls in Android With Clean Architecture

Minimize app crashes

David Sunday
Better Programming

--

Performing API calls in Android goes beyond checking the internet connection. Although several popular Open Source libraries are available such as Retrofit and Volley which help simplify these operations and error handling. But it’s important that you are able to format all possible errors that libraries will throw as a result of failure on these API calls.

This article will be showing you how to handle all error types that could be thrown while making an API call in order to prevent your app from crashing. For this demonstration I will be using the following:

  1. Coroutine Flow
  2. Dependency injection with Hilt
  3. Room and
  4. skydoves

Dependencies Integration:

Add the following dependencies to your build.gradle file(App module):

Configure your hilt in the plugin to use kapt and your build.gradle (project module). You can do this using the link I posted above. Please don't forget to add dependencies for retrofit and other libraries you might be using in your project.

Create a Data class:

For this sample, I will be using weather response data, just for showing a list of weather conditions in different locations.

Create an Interface

Copy and paste the code below to make a request to the weather API.

Note: I Am using ApiResponse as a wrapper for the weatherRemoteEntity data class instead of the normal Call from the Retrofit, this will give you the ability to format all possible responses that could be generated from making a call to that API.

Configure Retrofit Builder:

In other to get the DataSource from the Retrofit service we will be adding a CallAdapterFactory to our retrofit builder. So add the below code to your retrofit builder.

Create DataState:

Create a data state-sealed class for handling different states and emissions of responses. (Optional)

Create a Repository class:

This is where we are going to be doing all our error formatting and sending data to ViewModel for display in Activity. Below is what our repository will look like.

Sandwich gives you the ability to construct standardized interfaces from the Retrofit network response which allows you to handle the body data, errors, and exceptional cases obviously with useful operators in multi-layer architecture.

The ApiResponse has three main types; Success, Failure.Error, and Failure.Exception.

  • ApiResponse.Success — This represents the network request has been successful. You can get the body data of the response, and additional information such as StatusCode, Headers, and more from the ApiResponse.Success.
  • ApiResponse.Failure.Error — This represents the network request has been failed with bad requests or internal server errors. You can get an error message and additional information such as StatusCode, Headers, and more from the ApiResponse.Failure.Error. This covers almost all the server responses that can be thrown. So you could check for the error responses and format them appropriately. Below is a list of them.
  • ApiResponse.Failure.Exception— This represents the network request has been failed when unexpected exceptions occur while creating requests or processing a response from the client-side such as network connection failed. You can get an exception message from the ApiResponse.Failure.Exception and also format appropriately to avoid app crashes and to show suited messages.

In addition, the skydoves library can be used in other different ways to achieve a smooth error formatting and handling of API responses apart from the format I have demonstrated above. You can check out the link below.

Below is also an attachment of the complete implementation of the weather app, feel free to clone and check it out.

Thanks for reading, Happy coding.

--

--