Skip to content

Commit 2ee3f99

Browse files
jerzykrlkkdavisk6
authored andcommitted
Fine-grained HTTP error exceptions with client and server errors (OpenFeign#854)
1 parent 29df3e4 commit 2ee3f99

2 files changed

Lines changed: 56 additions & 17 deletions

File tree

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

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ public static FeignException errorStatus(String methodKey, Response response) {
8787
}
8888

8989
private static FeignException errorStatus(int status, String message, byte[] body) {
90+
if (isClientError(status)) {
91+
return clientErrorStatus(status, message, body);
92+
}
93+
if (isServerError(status)) {
94+
return serverErrorStatus(status, message, body);
95+
}
96+
return new FeignException(status, message, body);
97+
}
98+
99+
private static boolean isClientError(int status) {
100+
return status >= 400 && status < 500;
101+
}
102+
103+
private static FeignClientException clientErrorStatus(int status, String message, byte[] body) {
90104
switch (status) {
91105
case 400:
92106
return new BadRequest(message, body);
@@ -110,6 +124,17 @@ private static FeignException errorStatus(int status, String message, byte[] bod
110124
return new TooManyRequests(message, body);
111125
case 422:
112126
return new UnprocessableEntity(message, body);
127+
default:
128+
return new FeignClientException(status, message, body);
129+
}
130+
}
131+
132+
private static boolean isServerError(int status) {
133+
return status >= 500 && status <= 599;
134+
}
135+
136+
private static FeignServerException serverErrorStatus(int status, String message, byte[] body) {
137+
switch (status) {
113138
case 500:
114139
return new InternalServerError(message, body);
115140
case 501:
@@ -121,7 +146,7 @@ private static FeignException errorStatus(int status, String message, byte[] bod
121146
case 504:
122147
return new GatewayTimeout(message, body);
123148
default:
124-
return new FeignException(status, message, body);
149+
return new FeignServerException(status, message, body);
125150
}
126151
}
127152

@@ -134,97 +159,109 @@ static FeignException errorExecuting(Request request, IOException cause) {
134159
null);
135160
}
136161

137-
public static class BadRequest extends FeignException {
162+
public static class FeignClientException extends FeignException {
163+
public FeignClientException(int status, String message, byte[] body) {
164+
super(status, message, body);
165+
}
166+
}
167+
168+
public static class BadRequest extends FeignClientException {
138169
public BadRequest(String message, byte[] body) {
139170
super(400, message, body);
140171
}
141172
}
142173

143-
public static class Unauthorized extends FeignException {
174+
public static class Unauthorized extends FeignClientException {
144175
public Unauthorized(String message, byte[] body) {
145176
super(401, message, body);
146177
}
147178
}
148179

149-
public static class Forbidden extends FeignException {
180+
public static class Forbidden extends FeignClientException {
150181
public Forbidden(String message, byte[] body) {
151182
super(403, message, body);
152183
}
153184
}
154185

155-
public static class NotFound extends FeignException {
186+
public static class NotFound extends FeignClientException {
156187
public NotFound(String message, byte[] body) {
157188
super(404, message, body);
158189
}
159190
}
160191

161-
public static class MethodNotAllowed extends FeignException {
192+
public static class MethodNotAllowed extends FeignClientException {
162193
public MethodNotAllowed(String message, byte[] body) {
163194
super(405, message, body);
164195
}
165196
}
166197

167-
public static class NotAcceptable extends FeignException {
198+
public static class NotAcceptable extends FeignClientException {
168199
public NotAcceptable(String message, byte[] body) {
169200
super(406, message, body);
170201
}
171202
}
172203

173-
public static class Conflict extends FeignException {
204+
public static class Conflict extends FeignClientException {
174205
public Conflict(String message, byte[] body) {
175206
super(409, message, body);
176207
}
177208
}
178209

179-
public static class Gone extends FeignException {
210+
public static class Gone extends FeignClientException {
180211
public Gone(String message, byte[] body) {
181212
super(410, message, body);
182213
}
183214
}
184215

185-
public static class UnsupportedMediaType extends FeignException {
216+
public static class UnsupportedMediaType extends FeignClientException {
186217
public UnsupportedMediaType(String message, byte[] body) {
187218
super(415, message, body);
188219
}
189220
}
190221

191-
public static class TooManyRequests extends FeignException {
222+
public static class TooManyRequests extends FeignClientException {
192223
public TooManyRequests(String message, byte[] body) {
193224
super(429, message, body);
194225
}
195226
}
196227

197-
public static class UnprocessableEntity extends FeignException {
228+
public static class UnprocessableEntity extends FeignClientException {
198229
public UnprocessableEntity(String message, byte[] body) {
199230
super(422, message, body);
200231
}
201232
}
202233

203-
public static class InternalServerError extends FeignException {
234+
public static class FeignServerException extends FeignException {
235+
public FeignServerException(int status, String message, byte[] body) {
236+
super(status, message, body);
237+
}
238+
}
239+
240+
public static class InternalServerError extends FeignServerException {
204241
public InternalServerError(String message, byte[] body) {
205242
super(500, message, body);
206243
}
207244
}
208245

209-
public static class NotImplemented extends FeignException {
246+
public static class NotImplemented extends FeignServerException {
210247
public NotImplemented(String message, byte[] body) {
211248
super(501, message, body);
212249
}
213250
}
214251

215-
public static class BadGateway extends FeignException {
252+
public static class BadGateway extends FeignServerException {
216253
public BadGateway(String message, byte[] body) {
217254
super(502, message, body);
218255
}
219256
}
220257

221-
public static class ServiceUnavailable extends FeignException {
258+
public static class ServiceUnavailable extends FeignServerException {
222259
public ServiceUnavailable(String message, byte[] body) {
223260
super(503, message, body);
224261
}
225262
}
226263

227-
public static class GatewayTimeout extends FeignException {
264+
public static class GatewayTimeout extends FeignServerException {
228265
public GatewayTimeout(String message, byte[] body) {
229266
super(504, message, body);
230267
}

‎core/src/test/java/feign/codec/DefaultErrorDecoderHttpErrorTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public static Object[][] errorCodes() {
4242
{409, FeignException.Conflict.class},
4343
{429, FeignException.TooManyRequests.class},
4444
{422, FeignException.UnprocessableEntity.class},
45+
{450, FeignException.FeignClientException.class},
4546
{500, FeignException.InternalServerError.class},
4647
{501, FeignException.NotImplemented.class},
4748
{502, FeignException.BadGateway.class},
4849
{503, FeignException.ServiceUnavailable.class},
4950
{504, FeignException.GatewayTimeout.class},
51+
{599, FeignException.FeignServerException.class},
5052
{599, FeignException.class},
5153
};
5254
}

0 commit comments

Comments
 (0)