Feign Error Handling with ErrorDecoder

In this tutorial I am going to share with you how you can use Feign ErrorDecoder to handle errors that take place when using Feign client in Microservices communication.

For step-by-step video beginner lessons that demonstrate how to do Feign error handling and also 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 you give 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 HTTP status code, Body of HTTP Response and also the Request object. You can use these details when handing an error message and preparing a response.

Below is an example of ErrorDecoder interface being implemented.

public class FeignErrorDecoder implements ErrorDecoder {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    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>"); 
                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:

public class PhotoAppApiApplication {

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

Now when 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 and you get a correct switch case executed and the error message is logged.

Feign Client HTTP Requests Logging

When working with Feign client it is also very helpful to enable HTTP Requests logging. Following this 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, have a look 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.