Skip to content

Commit

Permalink
refactor(retrofit/test): delete RetrofitException class and test and …
Browse files Browse the repository at this point in the history
…throw SpinnakerHttpException
  • Loading branch information
SheetalAtre committed Aug 22, 2023
1 parent 49d62ce commit b4a02b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 161 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,25 +93,27 @@ 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) {
this.retrofit2Response = syncResp;
public <T> SpinnakerHttpException(
retrofit2.Response<T> retrofit2Response, retrofit2.Retrofit retrofit) {
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 @@ -177,17 +176,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.

0 comments on commit b4a02b4

Please sign in to comment.