Skip to content

Commit

Permalink
feat(retrofit): add accessor to exception response body as a map
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetalAtre committed Jul 7, 2023
1 parent 59915e2 commit a2856c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Objects;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import org.springframework.http.HttpHeaders;
import retrofit.RetrofitError;
import retrofit.client.Response;
Expand Down Expand Up @@ -151,7 +151,6 @@ public SpinnakerHttpException newInstance(String message) {
return new SpinnakerHttpException(message, this);
}


@Getter
// Use JsonIgnoreProperties because some responses contain properties that
// cannot be mapped to the RetrofitErrorResponseBody class. If the default
Expand All @@ -168,13 +167,11 @@ private static final class RetrofitErrorResponseBody {
}
}

public Map<String, Object> getResponseBody(RuntimeException e ) {
if(this.response == null) {
RetrofitError exception = (RetrofitError) e;
return (Map<String, Object>) exception.getBodyAs(HashMap.class);
}else{
RetrofitException exception = (RetrofitException) e;
return (Map<String, Object>) exception.getErrorBodyAs(HashMap.class);
public Map<String, Object> getResponseBody(RuntimeException e) {
if (this.response != null) {
return (Map<String, Object>) ((RetrofitError) e).getBodyAs(HashMap.class);
} else {
return (Map<String, Object>) ((RetrofitException) e).getErrorBodyAs(HashMap.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ public void testSpinnakerServerException_NewInstance() {
public void testSpinnakerHttpExceptionFromRetrofitException() {
final String validJsonResponseBodyString = "{\"name\":\"test\"}";
ResponseBody responseBody =
ResponseBody.create(
MediaType.parse("application/json" + "; charset=utf-8"), validJsonResponseBodyString);
ResponseBody.create(
MediaType.parse("application/json" + "; charset=utf-8"), validJsonResponseBodyString);
retrofit2.Response response =
retrofit2.Response.error(HttpStatus.NOT_FOUND.value(), responseBody);
retrofit2.Response.error(HttpStatus.NOT_FOUND.value(), responseBody);

Retrofit retrofit2Service =
new Retrofit.Builder()
.baseUrl("http://localhost")
.addConverterFactory(JacksonConverterFactory.create())
.build();
new Retrofit.Builder()
.baseUrl("http://localhost")
.addConverterFactory(JacksonConverterFactory.create())
.build();
RetrofitException retrofitException = RetrofitException.httpError(response, retrofit2Service);
SpinnakerHttpException notFoundException = new SpinnakerHttpException(retrofitException);
assertEquals(HttpStatus.NOT_FOUND.value(), notFoundException.getResponseCode());
assertTrue(
notFoundException.getMessage().contains(String.valueOf(HttpStatus.NOT_FOUND.value())));
notFoundException.getMessage().contains(String.valueOf(HttpStatus.NOT_FOUND.value())));
}
}

0 comments on commit a2856c1

Please sign in to comment.