Skip to content

Commit d3665e4

Browse files
finnetrollekdavisk6
authored andcommitted
OpenFeignGH-845: Add Request to Feign Exception (OpenFeign#1039)
Fixes OpenFeign#845 This change allows Feign Exceptions to be created with the original request as an optional parameter. Changes include: * Request field added to FeignException * New constructors are defended from null in request argument * Tests to check null instead of request, null message updated
1 parent 91e9882 commit d3665e4

20 files changed

Lines changed: 269 additions & 151 deletions

File tree

core/src/main/java/feign/FeignException.java

Lines changed: 118 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package feign;
1515

1616
import static feign.Util.UTF_8;
17+
import static feign.Util.checkNotNull;
1718
import static java.lang.String.format;
1819
import java.io.IOException;
1920

@@ -22,30 +23,67 @@
2223
*/
2324
public class FeignException extends RuntimeException {
2425

26+
private static final String EXCEPTION_MESSAGE_TEMPLATE_NULL_REQUEST = "request should not be null";
2527
private static final long serialVersionUID = 0;
2628
private int status;
2729
private byte[] content;
30+
private Request request;
2831

2932
protected FeignException(int status, String message, Throwable cause) {
3033
super(message, cause);
3134
this.status = status;
35+
this.request = null;
3236
}
3337

3438
protected FeignException(int status, String message, Throwable cause, byte[] content) {
3539
super(message, cause);
3640
this.status = status;
3741
this.content = content;
42+
this.request = null;
3843
}
3944

4045
protected FeignException(int status, String message) {
4146
super(message);
4247
this.status = status;
48+
this.request = null;
4349
}
4450

4551
protected FeignException(int status, String message, byte[] content) {
4652
super(message);
4753
this.status = status;
4854
this.content = content;
55+
this.request = null;
56+
}
57+
58+
protected FeignException(int status, String message, Request request, Throwable cause) {
59+
super(message, cause);
60+
this.status = status;
61+
this.request = checkRequestNotNull(request);
62+
}
63+
64+
protected FeignException(int status, String message, Request request, Throwable cause,
65+
byte[] content) {
66+
super(message, cause);
67+
this.status = status;
68+
this.content = content;
69+
this.request = checkRequestNotNull(request);
70+
}
71+
72+
protected FeignException(int status, String message, Request request) {
73+
super(message);
74+
this.status = status;
75+
this.request = checkRequestNotNull(request);
76+
}
77+
78+
protected FeignException(int status, String message, Request request, byte[] content) {
79+
super(message);
80+
this.status = status;
81+
this.content = content;
82+
this.request = checkRequestNotNull(request);
83+
}
84+
85+
private Request checkRequestNotNull(Request request) {
86+
return checkNotNull(request, EXCEPTION_MESSAGE_TEMPLATE_NULL_REQUEST);
4987
}
5088

5189
public int status() {
@@ -56,6 +94,14 @@ public byte[] content() {
5694
return this.content;
5795
}
5896

97+
public Request request() {
98+
return this.request;
99+
}
100+
101+
public boolean hasRequest() {
102+
return (this.request != null);
103+
}
104+
59105
public String contentUTF8() {
60106
if (content != null) {
61107
return new String(content, UTF_8);
@@ -68,6 +114,7 @@ static FeignException errorReading(Request request, Response response, IOExcepti
68114
return new FeignException(
69115
response.status(),
70116
format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()),
117+
request,
71118
cause,
72119
request.requestBody().asBytes());
73120
}
@@ -83,70 +130,79 @@ public static FeignException errorStatus(String methodKey, Response response) {
83130
} catch (IOException ignored) { // NOPMD
84131
}
85132

86-
return errorStatus(response.status(), message, body);
133+
return errorStatus(response.status(), message, response.request(), body);
87134
}
88135

89-
private static FeignException errorStatus(int status, String message, byte[] body) {
136+
private static FeignException errorStatus(int status,
137+
String message,
138+
Request request,
139+
byte[] body) {
90140
if (isClientError(status)) {
91-
return clientErrorStatus(status, message, body);
141+
return clientErrorStatus(status, message, request, body);
92142
}
93143
if (isServerError(status)) {
94-
return serverErrorStatus(status, message, body);
144+
return serverErrorStatus(status, message, request, body);
95145
}
96-
return new FeignException(status, message, body);
146+
return new FeignException(status, message, request, body);
97147
}
98148

99149
private static boolean isClientError(int status) {
100150
return status >= 400 && status < 500;
101151
}
102152

103-
private static FeignClientException clientErrorStatus(int status, String message, byte[] body) {
153+
private static FeignClientException clientErrorStatus(int status,
154+
String message,
155+
Request request,
156+
byte[] body) {
104157
switch (status) {
105158
case 400:
106-
return new BadRequest(message, body);
159+
return new BadRequest(message, request, body);
107160
case 401:
108-
return new Unauthorized(message, body);
161+
return new Unauthorized(message, request, body);
109162
case 403:
110-
return new Forbidden(message, body);
163+
return new Forbidden(message, request, body);
111164
case 404:
112-
return new NotFound(message, body);
165+
return new NotFound(message, request, body);
113166
case 405:
114-
return new MethodNotAllowed(message, body);
167+
return new MethodNotAllowed(message, request, body);
115168
case 406:
116-
return new NotAcceptable(message, body);
169+
return new NotAcceptable(message, request, body);
117170
case 409:
118-
return new Conflict(message, body);
171+
return new Conflict(message, request, body);
119172
case 410:
120-
return new Gone(message, body);
173+
return new Gone(message, request, body);
121174
case 415:
122-
return new UnsupportedMediaType(message, body);
175+
return new UnsupportedMediaType(message, request, body);
123176
case 429:
124-
return new TooManyRequests(message, body);
177+
return new TooManyRequests(message, request, body);
125178
case 422:
126-
return new UnprocessableEntity(message, body);
179+
return new UnprocessableEntity(message, request, body);
127180
default:
128-
return new FeignClientException(status, message, body);
181+
return new FeignClientException(status, message, request, body);
129182
}
130183
}
131184

132185
private static boolean isServerError(int status) {
133186
return status >= 500 && status <= 599;
134187
}
135188

136-
private static FeignServerException serverErrorStatus(int status, String message, byte[] body) {
189+
private static FeignServerException serverErrorStatus(int status,
190+
String message,
191+
Request request,
192+
byte[] body) {
137193
switch (status) {
138194
case 500:
139-
return new InternalServerError(message, body);
195+
return new InternalServerError(message, request, body);
140196
case 501:
141-
return new NotImplemented(message, body);
197+
return new NotImplemented(message, request, body);
142198
case 502:
143-
return new BadGateway(message, body);
199+
return new BadGateway(message, request, body);
144200
case 503:
145-
return new ServiceUnavailable(message, body);
201+
return new ServiceUnavailable(message, request, body);
146202
case 504:
147-
return new GatewayTimeout(message, body);
203+
return new GatewayTimeout(message, request, body);
148204
default:
149-
return new FeignServerException(status, message, body);
205+
return new FeignServerException(status, message, request, body);
150206
}
151207
}
152208

@@ -156,114 +212,114 @@ static FeignException errorExecuting(Request request, IOException cause) {
156212
format("%s executing %s %s", cause.getMessage(), request.httpMethod(), request.url()),
157213
request.httpMethod(),
158214
cause,
159-
null);
215+
null, request);
160216
}
161217

162218
public static class FeignClientException extends FeignException {
163-
public FeignClientException(int status, String message, byte[] body) {
164-
super(status, message, body);
219+
public FeignClientException(int status, String message, Request request, byte[] body) {
220+
super(status, message, request, body);
165221
}
166222
}
167223

168224
public static class BadRequest extends FeignClientException {
169-
public BadRequest(String message, byte[] body) {
170-
super(400, message, body);
225+
public BadRequest(String message, Request request, byte[] body) {
226+
super(400, message, request, body);
171227
}
172228
}
173229

174230
public static class Unauthorized extends FeignClientException {
175-
public Unauthorized(String message, byte[] body) {
176-
super(401, message, body);
231+
public Unauthorized(String message, Request request, byte[] body) {
232+
super(401, message, request, body);
177233
}
178234
}
179235

180236
public static class Forbidden extends FeignClientException {
181-
public Forbidden(String message, byte[] body) {
182-
super(403, message, body);
237+
public Forbidden(String message, Request request, byte[] body) {
238+
super(403, message, request, body);
183239
}
184240
}
185241

186242
public static class NotFound extends FeignClientException {
187-
public NotFound(String message, byte[] body) {
188-
super(404, message, body);
243+
public NotFound(String message, Request request, byte[] body) {
244+
super(404, message, request, body);
189245
}
190246
}
191247

192248
public static class MethodNotAllowed extends FeignClientException {
193-
public MethodNotAllowed(String message, byte[] body) {
194-
super(405, message, body);
249+
public MethodNotAllowed(String message, Request request, byte[] body) {
250+
super(405, message, request, body);
195251
}
196252
}
197253

198254
public static class NotAcceptable extends FeignClientException {
199-
public NotAcceptable(String message, byte[] body) {
200-
super(406, message, body);
255+
public NotAcceptable(String message, Request request, byte[] body) {
256+
super(406, message, request, body);
201257
}
202258
}
203259

204260
public static class Conflict extends FeignClientException {
205-
public Conflict(String message, byte[] body) {
206-
super(409, message, body);
261+
public Conflict(String message, Request request, byte[] body) {
262+
super(409, message, request, body);
207263
}
208264
}
209265

210266
public static class Gone extends FeignClientException {
211-
public Gone(String message, byte[] body) {
212-
super(410, message, body);
267+
public Gone(String message, Request request, byte[] body) {
268+
super(410, message, request, body);
213269
}
214270
}
215271

216272
public static class UnsupportedMediaType extends FeignClientException {
217-
public UnsupportedMediaType(String message, byte[] body) {
218-
super(415, message, body);
273+
public UnsupportedMediaType(String message, Request request, byte[] body) {
274+
super(415, message, request, body);
219275
}
220276
}
221277

222278
public static class TooManyRequests extends FeignClientException {
223-
public TooManyRequests(String message, byte[] body) {
224-
super(429, message, body);
279+
public TooManyRequests(String message, Request request, byte[] body) {
280+
super(429, message, request, body);
225281
}
226282
}
227283

228284
public static class UnprocessableEntity extends FeignClientException {
229-
public UnprocessableEntity(String message, byte[] body) {
230-
super(422, message, body);
285+
public UnprocessableEntity(String message, Request request, byte[] body) {
286+
super(422, message, request, body);
231287
}
232288
}
233289

234290
public static class FeignServerException extends FeignException {
235-
public FeignServerException(int status, String message, byte[] body) {
236-
super(status, message, body);
291+
public FeignServerException(int status, String message, Request request, byte[] body) {
292+
super(status, message, request, body);
237293
}
238294
}
239295

240296
public static class InternalServerError extends FeignServerException {
241-
public InternalServerError(String message, byte[] body) {
242-
super(500, message, body);
297+
public InternalServerError(String message, Request request, byte[] body) {
298+
super(500, message, request, body);
243299
}
244300
}
245301

246302
public static class NotImplemented extends FeignServerException {
247-
public NotImplemented(String message, byte[] body) {
248-
super(501, message, body);
303+
public NotImplemented(String message, Request request, byte[] body) {
304+
super(501, message, request, body);
249305
}
250306
}
251307

252308
public static class BadGateway extends FeignServerException {
253-
public BadGateway(String message, byte[] body) {
254-
super(502, message, body);
309+
public BadGateway(String message, Request request, byte[] body) {
310+
super(502, message, request, body);
255311
}
256312
}
257313

258314
public static class ServiceUnavailable extends FeignServerException {
259-
public ServiceUnavailable(String message, byte[] body) {
260-
super(503, message, body);
315+
public ServiceUnavailable(String message, Request request, byte[] body) {
316+
super(503, message, request, body);
261317
}
262318
}
263319

264320
public static class GatewayTimeout extends FeignServerException {
265-
public GatewayTimeout(String message, byte[] body) {
266-
super(504, message, body);
321+
public GatewayTimeout(String message, Request request, byte[] body) {
322+
super(504, message, request, body);
267323
}
268324
}
269325
}

core/src/main/java/feign/Response.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ public void close() throws IOException {
281281

282282
@Override
283283
public String toString() {
284-
try {
285-
return new String(toByteArray(inputStream), UTF_8);
286-
} catch (Exception e) {
287-
return super.toString();
288-
}
284+
try {
285+
return new String(toByteArray(inputStream), UTF_8);
286+
} catch (Exception e) {
287+
return super.toString();
288+
}
289289
}
290290
}
291291

core/src/main/java/feign/RetryableException.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ public class RetryableException extends FeignException {
3131
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
3232
*/
3333
public RetryableException(int status, String message, HttpMethod httpMethod, Throwable cause,
34-
Date retryAfter) {
35-
super(status, message, cause);
34+
Date retryAfter, Request request) {
35+
super(status, message, request, cause);
3636
this.httpMethod = httpMethod;
3737
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
3838
}
3939

4040
/**
4141
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
4242
*/
43-
public RetryableException(int status, String message, HttpMethod httpMethod, Date retryAfter) {
44-
super(status, message);
43+
public RetryableException(int status, String message, HttpMethod httpMethod, Date retryAfter,
44+
Request request) {
45+
super(status, message, request);
4546
this.httpMethod = httpMethod;
4647
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
4748
}

0 commit comments

Comments
 (0)