Package feign.codec

Interface ErrorDecoder

  • All Known Implementing Classes:
    ErrorDecoder.Default

    public interface ErrorDecoder
    Allows you to massage an exception into a application-specific one. Converting out to a throttle exception are examples of this in use.

    Ex:

     class IllegalArgumentExceptionOn404Decoder implements ErrorDecoder {
    
       @Override
       public Exception decode(String methodKey, Response response) {
         if (response.status() == 400)
           throw new IllegalArgumentException("bad zone name");
         return new ErrorDecoder.Default().decode(methodKey, response);
       }
    
     }
     

    Error handling

    Responses where Response.status() is not in the 2xx range are classified as errors, addressed by the ErrorDecoder. That said, certain RPC apis return errors defined in the Response.body() even on a 200 status. For example, in the DynECT api, a job still running condition is returned with a 200 status, encoded in json. When scenarios like this occur, you should raise an application-specific exception (which may be retryable).

    Not Found Semantics

    It is commonly the case that 404 (Not Found) status has semantic value in HTTP apis. While the default behavior is to raise exeception, users can alternatively enable 404 processing via Feign.Builder.decode404().

    • Method Detail

      • decode

        java.lang.Exception decode​(java.lang.String methodKey,
                                   Response response)
        Implement this method in order to decode an HTTP Response when Response.status() is not in the 2xx range. Please raise application-specific exceptions where possible. If your exception is retryable, wrap or subclass RetryableException
        Parameters:
        methodKey - Feign.configKey(java.lang.Class, java.lang.reflect.Method) of the java method that invoked the request. ex. IAM#getUser()
        response - HTTP response where status is greater than or equal to 300.
        Returns:
        Exception IOException, if there was a network error reading the response or an application-specific exception decoded by the implementation. If the throwable is retryable, it should be wrapped, or a subtype of RetryableException