Skip to content

Commit ef4b987

Browse files
sfellingersfellinger
authored andcommitted
feign.Request object now supported as exception constructor parameter
The ExceptionGenerator now * also looks for constructor parameter of type feign.Request. * creates the exception providing the feign.Request by calling response.request()
1 parent 958680b commit ef4b987

File tree

4 files changed

+264
-25
lines changed

4 files changed

+264
-25
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: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ 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, Integer numOfParams, Type bodyType,
5657
Class<? extends Exception> exceptionType, Decoder bodyDecoder) {
5758
this.bodyIndex = bodyIndex;
59+
this.requestIndex = requestIndex;
5860
this.headerMapIndex = headerMapIndex;
5961
this.numOfParams = numOfParams;
6062
this.bodyType = bodyType;
@@ -72,6 +74,10 @@ Exception createException(Response response) throws InvocationTargetException,
7274
paramClasses[bodyIndex] = Types.getRawType(bodyType);
7375
paramValues[bodyIndex] = resolveBody(response);
7476
}
77+
if (requestIndex >= 0) {
78+
paramClasses[requestIndex] = Request.class;
79+
paramValues[requestIndex] = response.request();
80+
}
7581
if (headerMapIndex >= 0) {
7682
paramValues[headerMapIndex] = response.headers();
7783
paramClasses[headerMapIndex] = Map.class;
@@ -120,6 +126,7 @@ public ExceptionGenerator build() {
120126
Annotation[][] parametersAnnotations = constructor.getParameterAnnotations();
121127

122128
Integer bodyIndex = -1;
129+
Integer requestIndex = -1;
123130
Integer headerMapIndex = -1;
124131
Integer numOfParams = parameterTypes.length;
125132
Type bodyType = null;
@@ -139,15 +146,22 @@ public ExceptionGenerator build() {
139146
}
140147
}
141148
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];
149+
if(parameterTypes[i].equals(Request.class)) {
150+
checkState(requestIndex == -1,
151+
"Cannot have two parameters either without annotations or with object of type feign.Request");
152+
requestIndex = i;
153+
} else {
154+
checkState(bodyIndex == -1,
155+
"Cannot have two parameters either without annotations or with @ResponseBody annotation");
156+
bodyIndex = i;
157+
bodyType = parameterTypes[i];
158+
}
146159
}
147160
}
148161

149162
ExceptionGenerator generator = new ExceptionGenerator(
150163
bodyIndex,
164+
requestIndex,
151165
headerMapIndex,
152166
numOfParams,
153167
bodyType,

0 commit comments

Comments
 (0)