美文网首页
feign client抛出自定义异常

feign client抛出自定义异常

作者: 十毛tenmao | 来源:发表于2023-06-01 22:00 被阅读0次

    feign client远程调用时,如果遇到返回的http status不是200的情况,则会抛出异常feign.FeignException。但是这种异常不方便处理,所以最好还是根据错误信息抛出自定义异常

    抛出自定义异常

    • 自定义异常
    public class HwException extends RuntimeException {
        public HwException(String message) {
            super(message);
        }
    }
    
    • 定义ErrorDecoder
    public class HwNetApiErrorDecoder implements ErrorDecoder {
        final ObjectMapper mapper;
    
        public HwNetApiErrorDecoder(ObjectMapper mapper) {
            this.mapper = mapper;
        }
    
        @Override
        public Exception decode(String methodKey, Response response) {
            if (HttpStatus.OK.value() == response.status()) {
                //这里不应该出现,因为只有不是200的返回结果,才是错误
                FeignException exception = errorStatus(methodKey, response);
                throw new RuntimeException(exception);
            }
            try {
                HwNetResult<?> netRet = mapper.readValue(response.body().asInputStream(), HwNetResult.class);
                if (netRet.isSuccess()) {
                    throw new RuntimeException("return values is success but http status is not 200");
                }
                throw new HwException(netRet.getErrorCode());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    • Feign自定义配置:
    public class WxFeignConfiguration {
        @Bean
        public HwNetApiErrorDecoder hwNetApiErrorDecoder(ObjectMapper mapper) {
            return new HwNetApiErrorDecoder(mapper);
        }
    }
    

    效果

    • 当http请求返回400错误时,并且error_code="one_IE"时,则会抛出异常HwException: one_IE
    java.lang.IllegalStateException: Failed to execute ApplicationRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:749)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
        at com.wx.demo.server.WxkjDemoServerApplication.main(WxkjDemoServerApplication.java:34)
    Caused by: com.wx.hardware.huawei.api.service.HwException: one_IE
        at com.wx.hardware.huawei.api.service.HwNetApiErrorDecoder.decode(HwNetApiErrorDecoder.java:34)
        at feign.ResponseHandler.decodeError(ResponseHandler.java:136)
        at feign.ResponseHandler.handleResponse(ResponseHandler.java:70)
        at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:114)
        at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:70)
        at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:96)
        at com.sun.proxy.$Proxy77.deviceInfo(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
    

    相关文章

      网友评论

          本文标题:feign client抛出自定义异常

          本文链接:https://www.haomeiwen.com/subject/hxlpedtx.html