Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring feign client exception handling

I have some fiegn client to send request other micro service.

@FeignClient(name="userservice")
public interface UserClient {

    @RequestMapping(
            method= RequestMethod.GET,
                      path = "/userlist")
    String getUserByid(@RequestParam(value ="id") String id);

}

Now I am sending request like this

try {
    String responseData = userClient.getUserByid(id);
    return responseData;
    }

catch(FeignException e)
 {
 logger.error("Failed to get user", id);
}

catch (Exception e) 
{
 logger.error("Failed to get user", id);
}

Here the problem is if any FeignException happens I dont get any error code.

I need to send a corresponding error codes in other APIS to send to caller

So how to extract the error code? I want to extract error code and build a responseEntity

I got this code but dont know how exactly I can use in my function.

like image 912
kcoder Avatar asked Mar 06 '19 10:03

kcoder


People also ask

Which is better REST template or feign client?

Feign Client It's not only about ease code writing ? Providing Feign Client in Spring Boot toolbox not only for ease code writing purpose for developer instead of using RestTemplate, but also it has an architectural decision and design.

How do you communicate between Microservices using Feign client?

Let's implement the Feign in our project and invoke other microservices using Feign. Step 1: Select currency-conversion-service project. Step 2: Open the pom. xml and add the Feign dependency.

What is the difference between WebClient and feign client?

Spring WebClient is a non-blocking reactive client to make HTTP requests. Feign is a library which helps us to create declarative REST clients easily with annotations and it provides better abstraction when we need to call an external service in Microservices Architecture.


1 Answers

I'm late to party but here are my 2 cents. We had same use case to handle exceptions based on error code and we used custom ErrorDecoder.

public class CustomErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        String requestUrl = response.request().url();
        Response.Body responseBody = response.body();
        HttpStatus responseStatus = HttpStatus.valueOf(response.status());

        if (responseStatus.is5xxServerError()) {
            return new RestApiServerException(requestUrl, responseBody);
        } else if (responseStatus.is4xxClientError()) {
            return new RestApiClientException(requestUrl, responseBody);
        } else {
            return new Exception("Generic exception");
        }
    }
}

Return @Bean of above class in FeignClientConfiguration class.

public class MyFeignClientConfiguration {

    @Bean
    public ErrorDecoder errorDecoder() {
        return new CustomErrorDecoder();
    }
}

Use this as your config class for FeignClient.

@FeignClient(value = "myFeignClient", configuration = MyFeignClientConfiguration.class)

Then you can handle these exceptions using GlobalExceptionHandler.

like image 91
VaibS Avatar answered Oct 22 '22 00:10

VaibS