Skip to content

Custom error handling

Patrick van Dissel edited this page May 13, 2014 · 1 revision

By default Feign only throws FeignException for any error situation, but you probably want an application specific exception instead. This is easily done by providing your own implementation of feign.codec.ErrorDecoder to Feign.builder.errorDecoder().

An example of such an ErrorDecoder implementation could be as simple as:

public class StashErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        if (response.status() >= 400 && response.status() <= 499) {
            return new StashClientException(
                    response.status(),
                    response.reason()
            );
        }
        if (response.status() >= 500 && response.status() <= 599) {
            return new StashServerException(
                    response.status(),
                    response.reason()
            );
        }
        return errorStatus(methodKey, response);
    }
}

Which you can then provide in your Feign.builder() like so:

return Feign.builder()
                .errorDecoder(new StashErrorDecoder())
                .target(StashApi.class, url);
Clone this wiki locally