Feign Error Handling with ErrorDecoder

In this tutorial, I will share with you how you can use Feign ErrorDecoder to handle errors that occur when using Feign client in Microservices communication.

For step-by-step video beginner lessons demonstrating how to do Feign error handling and how to build Microservices with Spring Boot and Spring Cloud, have a look at this page: Spring Boot Microservices and Spring Cloud.

Setup Feign

To make sure your Feign client works well and the errors you are getting are not caused by an incorrect setup of your Feign client, please have a look at the following tutorial to learn how to add Feign to your Spring Boot project and make it work: Feign Client to Call Another Microservice.

Create ErrorDecoder

To be able to use ErrorDecoder, you will need to create a new Java class and make it implement ErrorDecoder interface. Implementing ErrorDecoder interface gives you access to Method Key and Response objects.

  • methodKey – will contain a Feign client class name and a method name,
  • Response – will allow you to access the HTTP status code, the Body of HTTP Response and the Request object. You can use these details when handling an error message and preparing a response.

Below is an example of ErrorDecoder interface being implemented.

@Component
public class FeignErrorDecoder implements ErrorDecoder {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public Exception decode(String methodKey, Response response) {
 
       
        switch (response.status()){
            case 400:
                     logger.error("Status code " + response.status() + ", methodKey = " + methodKey);
            case 404:
            {
                     logger.error("Error took place when using Feign client to send HTTP Request. Status code " + response.status() + ", methodKey = " + methodKey);
                    return new ResponseStatusException(HttpStatus.valueOf(response.status()), "<You can add error message description here>"); 
            }
            default:
                return new Exception(response.reason());
        } 
    }
    
}

Please note the use of @Component annotation. If not using @Component annotation, you can also create Feign ErrorDecoder as a @Bean the following way:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class PhotoAppApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(PhotoAppApiApplication.class, args);
    }
 
    @Bean
    public FeignErrorDecoder errorDecoder() {
        return new FeignErrorDecoder();
    }
    
} 

Now that your Feign ErrorDecoder interface is implemented, you can try using Feign client to send HTTP Request to a Web Service endpoint that does not exist and see if the 404 HTTP Status code is handled. You get a correct switch case executed, and the error message is logged.

Feign Client HTTP Requests Logging

When working with Feign clients, it is also very helpful to enable HTTP Requests logging. Read the following tutorial to learn how to enable Feign logging in your Spring Boot application:

If you need to see how it is all done in step-by-step video lessons, checkout out at this page: Spring Boot Microservices and Spring Cloud.

I hope this tutorial was helpful to you.

There are many very good online video courses that teach how to build Spring Boot Microservices with Spring Cloud. Have a look at the list below and see if you like any of them.


1 Comment on "Feign Error Handling with ErrorDecoder"

Leave a Reply

Your email address will not be published. Required fields are marked *