-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fine-grained HTTP error exceptions with client and server errors #854
Fine-grained HTTP error exceptions with client and server errors #854
Conversation
Looking over this PR and reviewing the comments, I think that you've discovered why something like this has not been added before. It's been our opinion that decisions such as what exception should be surfaced back to the caller and whether that exception should be retried should be left to the user of Feign. I made this same comment in #823. I think that exposing this level of detail back to the caller leaks too much of Feign's internals. Let's take a look at your example from #823 try {
return this.library.getBook(1);
} catch (HttpNotFoundException hce) {
// handle not found.
}
With that said, I do see the value in having more concrete error types in Feign, but I recommend that we limit it's visibility. One suggestion is to move this logic into a new public class StatusCodeErrorDecoder implements ErrorDecoder {
private final List<Class<StatusCodeException>> retryableStatusCodes = new ArrayList();
public StatusCodeErrorDecoder(Class<StatusCodeExceptions> ... retryableCodes) {
this.retryableStatusCodes = Arrays.asList(retryableCodes);
}
@Override
public Exception decode(String methodKey, Response response) {
StatusCodeException statusException = this.getStatusCodeExceptionFromResponse(response);
if (this.retryableStatusExceptions.contains(statusException.getClass()) {
// retry
throw new RetryableException(statusException);
} else {
// decode the status code exception into a domain exception
return this.decodeStatusException(methodKey, response, statusException);
}
}
protected Exception decodeStatusException(String methodKey, Response response, StatusCodeException statusException {
// default is to wrap and throw the status as a Feign exception
throw new FeignException(statusException);
}
// base class for all status code exceptions, encapsulated into this decoder to
// intentionally limit visibility and isolate usage to this decoder.
static class StatusCodeException {
}
// additional specific status code exceptions are defined here.
} Users can use this decoder in place of the default encoder or extend it to create their own, more specific decoders: public class CustomStatusCodeErrorDecoder extends StatusCodeErrorDecoder {
public CustomStatusCodeErrorDecoder() {
// retry service unavailable and timeouts
super(ServiceUnavailableException.class, GatewayTimeoutException.class);
}
@Override
protected Exception decodeStatusException(String methodKey, Response response, StatusCodeException statusException {
// in this case, I want to return a domain exception when the object was not found
if (statusException instanceof HttpNotFoundException) {
throw new BookNotFoundException();
}
// all others log and return a generic exception
throw new BookException(statusException);
}
} Using this approach, we can allow users of Feign to create more expressive Thoughts? |
Hi @kdavisk6 It's just a response to @rage-shadowman comment here: #825 (comment) . I am not sure if it is absolutely necessary to have these, but they might be useful. In my opinion, this pull request should not go anywhere beyond distinguising 4xx and 5xx errors. Just as they are in Spring Framework - a purely technical distinction. Personally, I'd stick to that., but the discussion grew exponentially. :) In terms of retrying - I think that it is a different, more complex thing. And should be moved into a different discussion. And probably be followed by a separate pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jerzykrlk If others are ok with moving forward without the retry decision, this is good from my perspective.
I'd like to accept this change for 10.3.0. However, this is a conflict. Can you please take a look? |
e5f916f
to
7918d7f
Compare
Hi. I've rebased. I think it should be fine now. |
No description provided.