Commit bfb2ab5b authored by suhuiguang's avatar suhuiguang

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

parent 6510191a
......@@ -36,6 +36,7 @@ public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
template.header("token", token);
template.header("product", product);
template.header("appKey", appKey);
template.header("clientType","feignClient");
}
}
package com.yeejoin.amos.supervision.business.feign;
import com.alibaba.fastjson.JSONObject;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
......@@ -8,21 +10,26 @@ import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
/**
* @author DELL
*/
@Slf4j
public class FeignClientErrorDecoder implements ErrorDecoder {
public class FeignClientErrorDecoder extends ErrorDecoder.Default {
@Override
public Exception decode(String methodKey, Response response) {
log.info("feign client response:", response);
String body = null;
Exception exception = super.decode(methodKey, response);
String message;
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) {
log.error("feign.IOException", e);
}
if (response.status() >= 400 && response.status() <= 500) {
return null;
}
return null;
return exception;
}
}
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