Netflix Feign - Propagate Status and Exception through Microservices

后端 未结 6 787
予麋鹿
予麋鹿 2021-02-05 05:04

I\'m using Netflix Feign to call to one operation of a Microservice A to other other operation of a Microservice B which validates a code using Spring Boot.

The operation

相关标签:
6条回答
  • 2021-02-05 05:11

    You could use a feign ErrorDecoder

    https://github.com/OpenFeign/feign/wiki/Custom-error-handling

    Here is an example

    public class MyErrorDecoder implements ErrorDecoder {
    
        private final ErrorDecoder defaultErrorDecoder = new Default();
    
        @Override
        public Exception decode(String methodKey, Response response) {
            if (response.status() >= 400 && response.status() <= 499) {
                return new MyBadRequestException();
            }
            return defaultErrorDecoder.decode(methodKey, response);
        }
    
    }
    

    For spring to pick up the ErrorDecoder you have to put it on the ApplicationContext:

    @Bean
    public MyErrorDecoder myErrorDecoder() {
      return new MyErrorDecoder();
    }
    
    0 讨论(0)
  • 2021-02-05 05:11

    Write your custom exception mapper and register it. You can customize responses.

    Complete example is here

    public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
    
        @Override
        public Response toResponse(Throwable ex) {
            return Response.status(500).entity(YOUR_RETURN_OBJ_HERE).build();
        }
    
    }
    
    0 讨论(0)
  • 2021-02-05 05:21

    What we do is as follows:

    Share common jar which contains exceptions with both microservices.

    1.) In microservices A convert exception to a DTO class lets say ErrorInfo. Which will contain all the attributes of your custom exception with a String exceptionType, which will contain exception class name.

    2.) When it is received at microservice B it will be handled by ErrorDecoder in microservice B and It will try to create an exception object from exceptionType as below:

    @Override
    public Exception decode(String methodKey, Response response) {       
    
    ErrorInfo errorInfo = objectMapper.readValue(details, ErrorInfo.class);
    Class exceptionClass;
    
    Exception decodedException;
    
    try {
    
        exceptionClass = Class.forName(errorInfo.getExceptionType());  
    
        decodedException = (Exception) exceptionClass.newInstance();
    
        return decodedException;
    
     }
    
     catch (ClassNotFoundException e) {
    
        return new PlatformExecutionException(details, errorInfo);
    
     }
      return defaultErrorDecoder.decode(methodKey, response);
     }
    
    0 讨论(0)
  • 2021-02-05 05:23

    Shameless plug for a little library I did that uses reflection to dynamically rethrow checked exceptions (and unchecked if they are on the Feign interface) based on an error code returned in the body of the response.

    More information on the readme : https://github.com/coveo/feign-error-decoder

    0 讨论(0)
  • 2021-02-05 05:30

    Since 2017 we've created a library that does this from annotations (making it fairly easy to, just like for requests/etc, to code this up by annotations).

    it basically allows you to code error handling as follows:

    @ErrorHandling(codeSpecific =
        {
            @ErrorCodes( codes = {401}, generate = UnAuthorizedException.class),
            @ErrorCodes( codes = {403}, generate = ForbiddenException.class),
            @ErrorCodes( codes = {404}, generate = UnknownItemException.class),
        },
        defaultException = ClassLevelDefaultException.class
    )
    interface GitHub {
    
        @ErrorHandling(codeSpecific =
            {
                @ErrorCodes( codes = {404}, generate = NonExistentRepoException.class),
                @ErrorCodes( codes = {502, 503, 504}, generate = RetryAfterCertainTimeException.class),
            },
            defaultException = FailedToGetContributorsException.class
        )
        @RequestLine("GET /repos/{owner}/{repo}/contributors")
        List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
    }
    

    You can find it in the OpenFeign organisation: https://github.com/OpenFeign/feign-annotation-error-decoder

    disclaimer: I'm a contributor to feign and the main dev for that error decoder.

    0 讨论(0)
  • 2021-02-05 05:31

    OpenFeign's FeignException doesn't bind to a specific HTTP status (i.e. doesn't use Spring's @ResponseStatus annotation), which makes Spring default to 500 whenever faced with a FeignException. That's okay because a FeignException can have numerous causes that can't be related to a particular HTTP status.

    However you can change the way that Spring handles FeignExceptions. Simply define an ExceptionHandler that handles the FeignException the way you need it (see here):

    @RestControllerAdvice
    public class GlobalExceptionHandler {
    
        @ExceptionHandler(FeignException.class)
        public String handleFeignStatusException(FeignException e, HttpServletResponse response) {
            response.setStatus(e.status());
            return "feignError";
        }
    
    }
    

    This example makes Spring return the same HTTP status that you received from Microservice B. You can go further and also return the original response body:

    response.getOutputStream().write(e.content());
    
    0 讨论(0)
提交回复
热议问题