Skip to content

Commit

Permalink
Merge pull request #23 from StefanFellinger/master
Browse files Browse the repository at this point in the history
feign.Request object now supported as exception constructor parameter
  • Loading branch information
saintf authored Jul 22, 2019
2 parents 958680b + b4e436a commit bf3f2c9
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 23 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Any exception can be used if they have a default constructor:
class DefaultConstructorException extends Exception {}
```

However, if you want to have parameters (such as the body in the error response or headers), you have to annotate its
However, if you want to have parameters (such as the feign.Request object or response body or response headers), you have to annotate its
constructor appropriately (the body annotation is optional, provided there aren't paramters which will clash)

All the following examples are valid exceptions:
Expand All @@ -97,6 +97,20 @@ class JustBody extends Exception {

}
}
class JustRequest extends Exception {

@FeignExceptionConstructor
public JustRequest(Request request) {

}
}
class RequestAndResponseBody extends Exception {

@FeignExceptionConstructor
public RequestAndResponseBody(Request request, String body) {

}
}
//Headers must be of type Map<String, Collection<String>>
class BodyAndHeaders extends Exception {

Expand All @@ -105,6 +119,13 @@ class BodyAndHeaders extends Exception {

}
}
class RequestAndResponseBodyAndHeaders extends Exception {

@FeignExceptionConstructor
public RequestAndResponseBodyAndHeaders(Request request, @ResponseBody String body, @ResponseHeaders Map<String, Collection<String>> headers) {

}
}
class JustHeaders extends Exception {

@FeignExceptionConstructor
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/feign/error/ExceptionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ class ExceptionGenerator {
}

private final Integer bodyIndex;
private final Integer requestIndex;
private final Integer headerMapIndex;
private final Integer numOfParams;
private final Type bodyType;
private final Class<? extends Exception> exceptionType;
private final Decoder bodyDecoder;

ExceptionGenerator(Integer bodyIndex, Integer headerMapIndex, Integer numOfParams, Type bodyType,
ExceptionGenerator(Integer bodyIndex, Integer requestIndex, Integer headerMapIndex,
Integer numOfParams, Type bodyType,
Class<? extends Exception> exceptionType, Decoder bodyDecoder) {
this.bodyIndex = bodyIndex;
this.requestIndex = requestIndex;
this.headerMapIndex = headerMapIndex;
this.numOfParams = numOfParams;
this.bodyType = bodyType;
Expand All @@ -72,6 +75,10 @@ Exception createException(Response response) throws InvocationTargetException,
paramClasses[bodyIndex] = Types.getRawType(bodyType);
paramValues[bodyIndex] = resolveBody(response);
}
if (requestIndex >= 0) {
paramClasses[requestIndex] = Request.class;
paramValues[requestIndex] = response.request();
}
if (headerMapIndex >= 0) {
paramValues[headerMapIndex] = response.headers();
paramClasses[headerMapIndex] = Map.class;
Expand Down Expand Up @@ -120,6 +127,7 @@ public ExceptionGenerator build() {
Annotation[][] parametersAnnotations = constructor.getParameterAnnotations();

Integer bodyIndex = -1;
Integer requestIndex = -1;
Integer headerMapIndex = -1;
Integer numOfParams = parameterTypes.length;
Type bodyType = null;
Expand All @@ -139,15 +147,22 @@ public ExceptionGenerator build() {
}
}
if (!foundAnnotation) {
checkState(bodyIndex == -1,
"Cannot have two parameters either without annotations or with @ResponseBody annotation");
bodyIndex = i;
bodyType = parameterTypes[i];
if (parameterTypes[i].equals(Request.class)) {
checkState(requestIndex == -1,
"Cannot have two parameters either without annotations or with object of type feign.Request");
requestIndex = i;
} else {
checkState(bodyIndex == -1,
"Cannot have two parameters either without annotations or with @ResponseBody annotation");
bodyIndex = i;
bodyType = parameterTypes[i];
}
}
}

ExceptionGenerator generator = new ExceptionGenerator(
bodyIndex,
requestIndex,
headerMapIndex,
numOfParams,
bodyType,
Expand Down
Loading

0 comments on commit bf3f2c9

Please sign in to comment.