Skip to content

Commit bf3f2c9

Browse files
authored
Merge pull request #23 from StefanFellinger/master
feign.Request object now supported as exception constructor parameter
2 parents 958680b + b4e436a commit bf3f2c9

File tree

4 files changed

+282
-23
lines changed

4 files changed

+282
-23
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Any exception can be used if they have a default constructor:
8585
class DefaultConstructorException extends Exception {}
8686
```
8787

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

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

9898
}
9999
}
100+
class JustRequest extends Exception {
101+
102+
@FeignExceptionConstructor
103+
public JustRequest(Request request) {
104+
105+
}
106+
}
107+
class RequestAndResponseBody extends Exception {
108+
109+
@FeignExceptionConstructor
110+
public RequestAndResponseBody(Request request, String body) {
111+
112+
}
113+
}
100114
//Headers must be of type Map<String, Collection<String>>
101115
class BodyAndHeaders extends Exception {
102116

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

106120
}
107121
}
122+
class RequestAndResponseBodyAndHeaders extends Exception {
123+
124+
@FeignExceptionConstructor
125+
public RequestAndResponseBodyAndHeaders(Request request, @ResponseBody String body, @ResponseHeaders Map<String, Collection<String>> headers) {
126+
127+
}
128+
}
108129
class JustHeaders extends Exception {
109130

110131
@FeignExceptionConstructor

src/main/java/feign/error/ExceptionGenerator.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ class ExceptionGenerator {
4646
}
4747

4848
private final Integer bodyIndex;
49+
private final Integer requestIndex;
4950
private final Integer headerMapIndex;
5051
private final Integer numOfParams;
5152
private final Type bodyType;
5253
private final Class<? extends Exception> exceptionType;
5354
private final Decoder bodyDecoder;
5455

55-
ExceptionGenerator(Integer bodyIndex, Integer headerMapIndex, Integer numOfParams, Type bodyType,
56+
ExceptionGenerator(Integer bodyIndex, Integer requestIndex, Integer headerMapIndex,
57+
Integer numOfParams, Type bodyType,
5658
Class<? extends Exception> exceptionType, Decoder bodyDecoder) {
5759
this.bodyIndex = bodyIndex;
60+
this.requestIndex = requestIndex;
5861
this.headerMapIndex = headerMapIndex;
5962
this.numOfParams = numOfParams;
6063
this.bodyType = bodyType;
@@ -72,6 +75,10 @@ Exception createException(Response response) throws InvocationTargetException,
7275
paramClasses[bodyIndex] = Types.getRawType(bodyType);
7376
paramValues[bodyIndex] = resolveBody(response);
7477
}
78+
if (requestIndex >= 0) {
79+
paramClasses[requestIndex] = Request.class;
80+
paramValues[requestIndex] = response.request();
81+
}
7582
if (headerMapIndex >= 0) {
7683
paramValues[headerMapIndex] = response.headers();
7784
paramClasses[headerMapIndex] = Map.class;
@@ -120,6 +127,7 @@ public ExceptionGenerator build() {
120127
Annotation[][] parametersAnnotations = constructor.getParameterAnnotations();
121128

122129
Integer bodyIndex = -1;
130+
Integer requestIndex = -1;
123131
Integer headerMapIndex = -1;
124132
Integer numOfParams = parameterTypes.length;
125133
Type bodyType = null;
@@ -139,15 +147,22 @@ public ExceptionGenerator build() {
139147
}
140148
}
141149
if (!foundAnnotation) {
142-
checkState(bodyIndex == -1,
143-
"Cannot have two parameters either without annotations or with @ResponseBody annotation");
144-
bodyIndex = i;
145-
bodyType = parameterTypes[i];
150+
if (parameterTypes[i].equals(Request.class)) {
151+
checkState(requestIndex == -1,
152+
"Cannot have two parameters either without annotations or with object of type feign.Request");
153+
requestIndex = i;
154+
} else {
155+
checkState(bodyIndex == -1,
156+
"Cannot have two parameters either without annotations or with @ResponseBody annotation");
157+
bodyIndex = i;
158+
bodyType = parameterTypes[i];
159+
}
146160
}
147161
}
148162

149163
ExceptionGenerator generator = new ExceptionGenerator(
150164
bodyIndex,
165+
requestIndex,
151166
headerMapIndex,
152167
numOfParams,
153168
bodyType,

0 commit comments

Comments
 (0)