Improving default exception handling to include response body.
diff --git a/src/main/java/io/mifos/core/api/util/AnnotatedErrorDecoder.java b/src/main/java/io/mifos/core/api/util/AnnotatedErrorDecoder.java
index cb6f0d5..af7387c 100644
--- a/src/main/java/io/mifos/core/api/util/AnnotatedErrorDecoder.java
+++ b/src/main/java/io/mifos/core/api/util/AnnotatedErrorDecoder.java
@@ -18,12 +18,14 @@
import feign.Feign;
import feign.FeignException;
import feign.Response;
+import feign.Util;
import feign.codec.ErrorDecoder;
import io.mifos.core.api.annotation.ThrowsException;
import io.mifos.core.api.annotation.ThrowsExceptions;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
+import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -60,16 +62,25 @@
}
private RuntimeException getAlternative(final String methodKey, final Response response) {
- if (response.status() == HttpStatus.BAD_REQUEST.value()) {
- return new IllegalArgumentException(response.reason());
- } else if (response.status() == HttpStatus.FORBIDDEN.value()) {
- return new InvalidTokenException(response.reason());
- } else if (response.status() == HttpStatus.NOT_FOUND.value()) {
- return new NotFoundException();
- } else if (response.status() == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
- return new InternalServerError(response.reason());
- } else {
+ try {
+ final String bodyText = Util.toString(response.body().asReader());
+
+ if (response.status() == HttpStatus.BAD_REQUEST.value()) {
+ return new IllegalArgumentException(bodyText);
+ } else if (response.status() == HttpStatus.FORBIDDEN.value()) {
+ return new InvalidTokenException(bodyText);
+ } else if (response.status() == HttpStatus.NOT_FOUND.value()) {
+ return new NotFoundException(bodyText);
+ } else if (response.status() == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
+ return new InternalServerError(bodyText);
+ } else {
+ return FeignException.errorStatus(methodKey, response);
+ }
+
+ } catch (IOException e) {
+
return FeignException.errorStatus(methodKey, response);
+
}
}
diff --git a/src/main/java/io/mifos/core/api/util/NotFoundException.java b/src/main/java/io/mifos/core/api/util/NotFoundException.java
index 9649cd5..98650e8 100644
--- a/src/main/java/io/mifos/core/api/util/NotFoundException.java
+++ b/src/main/java/io/mifos/core/api/util/NotFoundException.java
@@ -21,4 +21,7 @@
@SuppressWarnings("WeakerAccess")
public class NotFoundException extends RuntimeException {
+ public NotFoundException(final String reason) {
+ super(reason);
+ }
}