I want to be able to deserialize network response to the same Java object even if response is not succesfull. Currently when I get an error response like 403, the response body is null, and I'm expected to use the response.errorBody() method to read whatever has been sent back, which is ok. However I want to avoid having alot of code in the retrofit callbacks just for deserializing errorBody. Instead I want to have an interceptor that sets the content of errorBody into body.
The reason for this is that I have a response object that has fields for both error and non error responses and depending on the status of the response some fields are expected to be left null e.g
Error Response JSON
{
"locked":true,
"remaining_attempts": 2
}
Success Response JSON
{
"name":"kev"
"token":"abcdefghijklmnopq"
}
I have created a single Java object that captures both scenarios:
class LoginResponse{
@Expose
private String name;
@Expose
private String token;
@Expose
private Boolean locked;
@Expose
private Integer remaining_attempts;
}
Is there way to do this in an interceptor?