Skip to content

Commit

Permalink
feat(retrofit): give a custom parsing error message when fails to con…
Browse files Browse the repository at this point in the history
…vert SpinnakerHttpException responsebody
  • Loading branch information
SheetalAtre committed Jul 17, 2023
1 parent c7dc955 commit 851d704
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import org.springframework.http.HttpStatus;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
* The {@link RetrofitException} class is similar to {@link retrofit.RetrofitError} as RetrofitError
Expand All @@ -33,8 +37,7 @@
*/
public class RetrofitException extends RuntimeException {
public static RetrofitException httpError(Response response, Retrofit retrofit) {
String message = response.code() + " " + response.message();
return new RetrofitException(message, response, retrofit);
return new RetrofitException(response, retrofit);
}

/** Response from server, which contains causes for the failure */
Expand All @@ -46,17 +49,10 @@ public static RetrofitException httpError(Response response, Retrofit retrofit)
*/
private final Retrofit retrofit;

private static final ResponseBody jsonErrorResponse;
private static Map<String, Object> jsonErrorResponseBody;

static {
jsonErrorResponse =
ResponseBody.create(
MediaType.parse("application/json; charset=utf-8"),
"{\"message\":\"Failed to parse response\"}");
}

RetrofitException(String message, Response response, Retrofit retrofit) {
super(message);
RetrofitException(Response response, Retrofit retrofit) {
super(response.code() + " " + response.message());

this.response = response;
if (response != null) {
Expand All @@ -77,21 +73,38 @@ public Response getResponse() {
* @throws RuntimeException wrapping the underlying IOException if unable to convert the body to
* the specified {@code type}.
*/
public <T> T getErrorBodyAs(Class<T> type) {
public Map getErrorBodyAs() {
if (response == null) {
return null;
}

Converter<ResponseBody, T> converter = retrofit.responseBodyConverter(type, new Annotation[0]);
Converter<ResponseBody, Map> converter =
retrofit.responseBodyConverter(Map.class, new Annotation[0]);
try {
return converter.convert(response.errorBody());
} catch (IOException e) {
try {
return converter.convert(jsonErrorResponse);
} catch (IOException ex) {
// control is never expected to come to this block.
throw new RuntimeException(ex);
}
return jsonErrorResponseBody;
}
}

static {
getJsonErrorResponseBody();
}

private static void getJsonErrorResponseBody() {
ResponseBody jsonErrorResponse =
ResponseBody.create(
MediaType.parse("application/json; charset=utf-8"),
"{\"message\":\"Failed to parse response\"}");
retrofit2.Response response =
retrofit2.Response.error(HttpStatus.NOT_FOUND.value(), jsonErrorResponse);
Retrofit retrofit2Service =
new Retrofit.Builder()
.baseUrl("http://localhost")
.addConverterFactory(JacksonConverterFactory.create())
.build();

RetrofitException retrofitException = httpError(response, retrofit2Service);
jsonErrorResponseBody = (HashMap<String, Object>) retrofitException.getErrorBodyAs();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public SpinnakerHttpException(RetrofitException e) {
super(e);
this.response = null;
this.retrofit2Response = e.getResponse();
responseBody = (Map<String, Object>) e.getErrorBodyAs(HashMap.class);
responseBody = (Map<String, Object>) e.getErrorBodyAs();
this.rawMessage =
responseBody != null
? (String) responseBody.getOrDefault("message", e.getMessage())
Expand Down

0 comments on commit 851d704

Please sign in to comment.