Skip to content

Commit

Permalink
refactor(retrofit): delete RetrofitException class and test
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetalAtre committed Jul 19, 2023
1 parent bd86abb commit 63ffe23
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 165 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

/**
* An exception that exposes the {@link Response} of a given HTTP {@link RetrofitError} or {@link
* okhttp3.Response} of a {@link RetrofitException} if retrofit 2.x used and a detail message that
* extracts useful information from the {@link Response} or {@link okhttp3.Response}. Both {@link
* Response} and {@link okhttp3.Response} can't be set together..
* okhttp3.Response} if retrofit 2.x used and a detail message that extracts useful information from
* the {@link Response} or {@link okhttp3.Response}. Both {@link Response} and {@link
* okhttp3.Response} can't be set together..
*/
@NonnullByDefault
public class SpinnakerHttpException extends SpinnakerServerException {
Expand All @@ -49,17 +49,14 @@ public class SpinnakerHttpException extends SpinnakerServerException {
private final String rawMessage;

private final Map<String, Object> responseBody;
private retrofit2.Retrofit retrofit;

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

public SpinnakerHttpException(RetrofitError e) {
super(e);
this.response = e.getResponse();
this.retrofit2Response = null;
this.retrofit = null;
responseBody = (Map<String, Object>) e.getBodyAs(HashMap.class);

this.rawMessage =
responseBody != null
? (String) responseBody.getOrDefault("message", e.getMessage())
Expand Down Expand Up @@ -96,28 +93,28 @@ public SpinnakerHttpException(String message, SpinnakerHttpException cause) {
this.retrofit2Response = cause.retrofit2Response;
rawMessage = null;
this.responseBody = cause.responseBody;
this.retrofit = null;
}

/**
* The constructor handles the HTTP retrofit2 exception, similar to retrofit logic. It is used
* with {@link com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory}.
*/
public <T> SpinnakerHttpException(retrofit2.Response<T> syncResp, retrofit2.Retrofit retrofit) {
super(
syncResp.code() + " " + syncResp.message(),
new Throwable(syncResp.code() + " " + syncResp.message()));
this.retrofit2Response = syncResp;
public <T> SpinnakerHttpException(
retrofit2.Response<T> retrofit2Response, retrofit2.Retrofit retrofit) {
super(new Throwable(retrofit2Response.code() + " " + retrofit2Response.message()));
this.retrofit2Response = retrofit2Response;
this.response = null;
this.retrofit = retrofit;
responseBody = this.getErrorBodyAs();
this.rawMessage =
responseBody != null
? (String) responseBody.getOrDefault("message", getMessage())
: getMessage();
if ((retrofit2Response.code() == HttpStatus.NOT_FOUND.value())
|| (retrofit2Response.code() == HttpStatus.BAD_REQUEST.value())) {
setRetryable(false);
}
responseBody = (Map<String, Object>) this.getErrorBodyAs(HashMap.class);
this.rawMessage =
responseBody != null
? (String) responseBody.getOrDefault("message", getMessage())
: getMessage();
}

public int getResponseCode() {
Expand Down Expand Up @@ -178,17 +175,16 @@ public Map<String, Object> getResponseBody() {
return this.responseBody;
}

private Map<String, Object> getErrorBodyAs() {
public <T> T getErrorBodyAs(Class<T> type) {
if (retrofit2Response == null) {
return null;
}

Converter<ResponseBody, Map> converter =
retrofit.responseBodyConverter(Map.class, new Annotation[0]);
Converter<ResponseBody, T> converter = retrofit.responseBodyConverter(type, new Annotation[0]);
try {
return converter.convert(retrofit2Response.errorBody());
} catch (IOException e) {
return jsonErrorResponseBody;
throw new RuntimeException(e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void testSpinnakerHttpExceptionFromRetrofitException() {
"Status: %s, URL: %s, Message: %s",
HttpStatus.NOT_FOUND.value(),
"http://localhost/",
HttpStatus.NOT_FOUND.value() + " " + "Response.error()");
"java.lang.Throwable:" + " " + HttpStatus.NOT_FOUND.value() + " " + "Response.error()");
assertEquals(expectedMessage, notFoundException.getMessage());
}
}

0 comments on commit 63ffe23

Please sign in to comment.