【发布时间】:2020-10-08 16:52:17
【问题描述】:
据我了解,当请求以状态码 != 2xx 响应时,将调用 feign ErrorDecoder 的 decode() 方法。通过调试我的测试,我发现我的 CustomErrorDecoder 的 decode() 方法没有被调用,例如504 或 404。我尝试了两种配置方式:
要么将其作为 Bean 包含在客户端配置中:
@Bean
public CustomErrorDecoder customErrorDecoder() {
return new CustomErrorDecoder();
}
或将其写入应用程序配置:
feign:
client:
config:
myCustomRestClientName:
retryer: com.a.b.some.package.CustomRetryer
errorDecoder: com.a.b.some.package.CustomErrorDecoder
这两种方式都不会调用 ErrorDecoder。我究竟做错了什么? Bean 正在实例化,我的 CustomErrorDecoder 看起来像这样:
@Component
public class CustomErrorDecoder implements ErrorDecoder {
private final ErrorDecoder defaultErrorDecoder = new Default();
@Override
public Exception decode(String s, Response response) {
Exception exception = defaultErrorDecoder.decode(s, response);
if (exception instanceof RetryableException) {
return exception;
}
if (response.status() == 504) {
// throwing new RetryableException to retry 504s
}
return exception;
}
}
更新:
我在 this git repo 中创建了一个最小的可重现示例。请查看提交历史,找出我尝试过的 3 种方法。
【问题讨论】:
-
问题中的一切似乎都很好。你能分享minimal reproducible example吗?可能错误在其他地方......
-
@amseager 我已更新问题以包含指向 git 存储库的链接。
标签: spring error-handling spring-cloud-feign feign openfeign