How to manage Feign errors?

徘徊边缘 提交于 2021-02-07 18:18:21

问题


We are using Spring-boot with Spring-cloud and Spring-cloud-netflix with Spring-cloud-feign.

We are creating our Gateway application that with the help of Feign will try to communicate with our authentication microservice in order to validate their credentials. Here you can see an example of our Feign authentication client:

@FeignClient(value="auth", configuration = AuthClientConfiguration.class)

public interface AuthClient {
   @RequestMapping(method = RequestMethod.GET, value = "/tokens", consumes = MediaType.APPLICATION_JSON_VALUE)
   Single<Session> getSession(@RequestHeader("Authorization") String token);
}

The question is, how we can deal with all the exceptions that could be raised by the client? I mean, how we can for example catch that a NetworkException or a TimeoutException has been thrown? We've defined our own ErrorDecoder but it appears that this "kind of listener" only works when the request has arrived and the response returned (in our case from authentication client). So, how we can manage this other exceptions?

Best,


回答1:


Error decoders are decoding HTTP error responses (500, 404, 401, etc...). Exceptions will bubble up in client calls, so using try/catch should work.

    try {
        return client.home();
    } catch (RuntimeException e) {
        e.printStackTrace();
        throw e;
    }


来源:https://stackoverflow.com/questions/41767740/how-to-manage-feign-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!