How to catch FeignClient exception

27,599

You can't catch the exception of the FeignClient with a @ControllerAdvice. The exception handler will not catch the exceptions generated by the feign client, error decoder..

A simple solution would be catch your feign calls, and then throw the exception you want.

try{
   feignClient.method();
} catch(Exception ex){
  //throw exceptions you want
  throw new YourException();
}

Then you'll be able to handle it:

@ControllerAdvice
public class GlobalControllerAdvice {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(YourException.class)
    public ResponseEntity<RestApiException> handleException(RestApiException exception, HttpServletRequest req) {
        //impl
    }

}
Share:
27,599
koa73
Author by

koa73

just IT profy

Updated on August 22, 2020

Comments

  • koa73
    koa73 almost 4 years

    I try to catch exception wich I received from another microservice connected by FeignClient. I've made custom ErrorDecoder, and

    public class CustomErrorDecoder implements ErrorDecoder {
    
        private final Logger log = LoggerFactory.getLogger(getClass());
    
        private final ErrorDecoder defaultErrorDecoder = new Default();
    
        @Override
        public Exception decode(String methodKey, Response response) {
    
            if (response.status() >= 400 && response.status() <= 499) {
                log.info("----------------- "+methodKey+" , "+response.status()+" , "+ response.reason());
                return new RestApiException(99,99,"");
            }
            return defaultErrorDecoder.decode(methodKey, response);
        }
    } 
    

    Where RestApiException extends Exception.

    @ControllerAdvice
    public class GlobalControllerAdvice {
    
        private final Logger log = LoggerFactory.getLogger(getClass());
    
        @ExceptionHandler(RestApiException.class)
        public ResponseEntity<RestApiException> handleException(RestApiException exception, HttpServletRequest req) {
            log.error("Sending error to client ( "+req.getUserPrincipal().getName()+" ) \"{}\"", exception.getErrMsg());
            return new ResponseEntity<RestApiException>(exception, exception.getStatus());
        }
    
        @ExceptionHandler(Throwable.class)
        public ResponseEntity<RestApiException> handleException(Throwable throwable, HttpServletRequest req) {
            RestApiException exception=new RestApiException(HttpStatus.INTERNAL_SERVER_ERROR, 100, 100,
                    throwable.getMessage());
            return handleException(exception, req);
        }
    

    As a result, when I get <--- HTTP/1.1 400 Bad Request (5380ms) I've got default error messages

    HttpStatus.INTERNAL_SERVER_ERROR, 100, 100, throwable.getMessage());

    but not expexted custom exception, which I try to set in CustomErrorDecoder.

    What I'm doing wrong, why I can't call RetAppiException and return error answer to rest client.

    Thanks.