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 4febb41 commit a699bb5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +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 @@ -45,6 +50,9 @@ public static RetrofitException httpError(Response response, Retrofit retrofit)
*/
private final Retrofit retrofit;

private static Map<String, Object> jsonErrorResponseBody =
Map.of("message", "failed to parse response");

RetrofitException(String message, Response response, Retrofit retrofit) {
super(message);

Expand All @@ -67,16 +75,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<String, Object> 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) {
throw new RuntimeException(e);
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ public void testSpinnakerHttpExceptionFromRetrofitException() {
Map<String, Object> errorResponseBody = notFoundException.getResponseBody();
assertEquals(errorResponseBody.get("name"), "test");
assertEquals(HttpStatus.NOT_FOUND.value(), notFoundException.getResponseCode());
assertTrue(
notFoundException.getMessage().contains(String.valueOf(HttpStatus.NOT_FOUND.value())));

// Note: Expect custom "Failed to parse response" message,
// instead of a RuntimeException when the converter fails to convert the response body. eg:
// JacksonResponseBodyConverter returns JsonParseException/JsonEOFException which are type of
// RuntimeException.
String expectedMessage =
String.format(
"Status: %s, URL: %s, Message: %s",
HttpStatus.NOT_FOUND.value(),
"http://localhost/",
HttpStatus.NOT_FOUND.value() + " " + "Response.error()");
assertEquals(expectedMessage, notFoundException.getMessage());
}
}

0 comments on commit a699bb5

Please sign in to comment.