Retrofit 2.0 how to get deserialised error response.body
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Retrofit 2.0: Handling Deserialized Error Responses
Retrofit 2.0 is a robust HTTP client for Android and Java developed by Square, designed to ease the process of making network calls and handling server responses. While Retrofit provides seamless support for successful API calls, dealing with error responses requires additional steps to ensure the error data, often contained in the response body, is properly deserialized and handled. This article delves into the specifics of how to handle deserialized error responses in Retrofit 2.0.
Understanding Error Responses
When a server responds with an error status code (e.g., 400, 404, 500), Retrofit treats these responses differently compared to successful responses (e.g., 200). By default, the Response object in Retrofit only contains an error body if an error occurs, and this body needs to be accessed and interpreted manually.
Deserializing Error Body with Retrofit
To deserialize an error response in Retrofit, you typically need an error model class that matches the expected structure of the error JSON. Retrofit provides converters like Gson, Moshi, or Jackson which can automate the parsing of JSON data into Java objects. Here we demonstrate handling error responses using Gson.
Step-by-Step Error Deserialization
- Define the Error Response Model:Define a class to represent the structure of the error body which you expect from the server. For example:
- Create a Method to Deserialize the Error Body:Implement a utility method to extract and deserialize the error body from the response:
- Handle the Response in API Callback:Use the custom error response handling within your API call's response callback:
Summary of Important Concepts
Here's a summary of key points related to deserializing error responses with Retrofit 2.0:
| Concept | Explanation |
| Converter | Turns JSON into Java objects or Java objects into JSON using a JSON converter. |
| ErrorResponse Model | Represents the expected structure of the error body in JSON format. |
| Response.isSuccessful() | Checks if the HTTP response was successful (i.e., response code is between 200-299). |
| Response.errorBody() | Contains the raw error body returned by the server. |
| Custom Deserialization | Implement your method for handling and interpreting the error body. |
Considerations and Best Practices
- Use Standardized Error Structures: Consistent error response structures from your API can simplify error handling.
- Logging for Debugging: Always log error responses during development to understand and debug issues effectively.
- Exception Handling: Ensure proper exception handling when parsing the response to prevent crashes from unexpected structures or formats.
- Testing: Use unit tests to validate error handling logic and confirm correct parsing and handling of different error scenarios.
Additional Topics of Interest
- Dynamic Error Handling: Explore dynamic models for flexible handling of varying error structures.
- Advanced Parsing with Moshi or Jackson: Consider alternative parsers based on your project requirements and preferences.
- Global Error Handling Strategy: Implement Interceptors or custom OkHttpClient to manage error responses globally.
By understanding the nuances of error handling and leveraging Retrofit's support for custom converters, you can build more resilient Android and Java applications. Proper handling ensures users receive informative feedback, even in the face of network uncertainties.

