You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by ju...@apache.org on 2019/10/09 07:39:04 UTC
[fineract-cn-api] 03/38: Improving default exception handling to
include response body.
This is an automated email from the ASF dual-hosted git repository.
juhan pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract-cn-api.git
commit 601a8d09531b4132b8c1e7f5ca55f910b4814aef
Author: myrle-krantz <mk...@mifos.org>
AuthorDate: Mon Mar 27 18:19:29 2017 +0200
Improving default exception handling to include response body.
---
.../mifos/core/api/util/AnnotatedErrorDecoder.java | 29 +++++++++++++++-------
.../io/mifos/core/api/util/NotFoundException.java | 3 +++
2 files changed, 23 insertions(+), 9 deletions(-)
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 @@ package io.mifos.core.api.util;
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 @@ class AnnotatedErrorDecoder implements ErrorDecoder {
}
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 @@ package io.mifos.core.api.util;
@SuppressWarnings("WeakerAccess")
public class NotFoundException extends RuntimeException {
+ public NotFoundException(final String reason) {
+ super(reason);
+ }
}