Commit bfb2ab5b authored by suhuiguang's avatar suhuiguang

1.feign调用异常,增加抛出错误方信息

parent 6510191a
...@@ -36,6 +36,7 @@ public class FeignBasicAuthRequestInterceptor implements RequestInterceptor { ...@@ -36,6 +36,7 @@ public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
template.header("token", token); template.header("token", token);
template.header("product", product); template.header("product", product);
template.header("appKey", appKey); template.header("appKey", appKey);
template.header("clientType","feignClient");
} }
} }
package com.yeejoin.amos.supervision.business.feign; package com.yeejoin.amos.supervision.business.feign;
import com.alibaba.fastjson.JSONObject;
import feign.FeignException;
import feign.Response; import feign.Response;
import feign.Util; import feign.Util;
import feign.codec.ErrorDecoder; import feign.codec.ErrorDecoder;
...@@ -8,21 +10,26 @@ import lombok.extern.slf4j.Slf4j; ...@@ -8,21 +10,26 @@ import lombok.extern.slf4j.Slf4j;
import java.io.IOException; import java.io.IOException;
/**
* @author DELL
*/
@Slf4j @Slf4j
public class FeignClientErrorDecoder implements ErrorDecoder { public class FeignClientErrorDecoder extends ErrorDecoder.Default {
@Override @Override
public Exception decode(String methodKey, Response response) { public Exception decode(String methodKey, Response response) {
log.info("feign client response:", response); Exception exception = super.decode(methodKey, response);
String body = null; String message;
try { try {
body = Util.toString(response.body().asReader()); if (exception instanceof FeignException && ((FeignException) exception).responseBody().isPresent()) {
String body = Util.toString(response.body().asReader());
JSONObject jsonObject = JSONObject.parseObject(body);
message = jsonObject.getString("devMessage");
return new RuntimeException(message);
}
} catch (IOException e) { } catch (IOException e) {
log.error("feign.IOException", e); log.error("feign.IOException", e);
} }
if (response.status() >= 400 && response.status() <= 500) { return exception;
return null;
}
return null;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment