|
@@ -1,10 +1,12 @@
|
|
|
package com.mrxu.framework.boot.handle;
|
|
package com.mrxu.framework.boot.handle;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.mrxu.framework.boot.feign.FeignProviderException;
|
|
|
import com.mrxu.framework.common.util.BusinessException;
|
|
import com.mrxu.framework.common.util.BusinessException;
|
|
|
import feign.Response;
|
|
import feign.Response;
|
|
|
import feign.Util;
|
|
import feign.Util;
|
|
|
import feign.codec.ErrorDecoder;
|
|
import feign.codec.ErrorDecoder;
|
|
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Import;
|
|
import org.springframework.context.annotation.Import;
|
|
@@ -12,6 +14,11 @@ import org.springframework.context.annotation.Import;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.lang.annotation.*;
|
|
import java.lang.annotation.*;
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * @Author: xujunwei
|
|
|
|
|
+ * @Date: 2022/5/16
|
|
|
|
|
+ * @Description: rpc 调用方 处理500错误时的编码
|
|
|
|
|
+ */
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Configuration
|
|
@Configuration
|
|
|
public class FeignErrorDecoder implements ErrorDecoder {
|
|
public class FeignErrorDecoder implements ErrorDecoder {
|
|
@@ -19,19 +26,26 @@ public class FeignErrorDecoder implements ErrorDecoder {
|
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
|
@Target({ElementType.TYPE})
|
|
@Target({ElementType.TYPE})
|
|
|
@Documented
|
|
@Documented
|
|
|
- @Import({FeignErrorDecoder.class})
|
|
|
|
|
|
|
+ @Import({FeignErrorDecoder.class,FeignProviderExceptionHandler.class})
|
|
|
public @interface EnableFeignErrorDecoder {
|
|
public @interface EnableFeignErrorDecoder {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
@Override
|
|
@Override
|
|
|
public Exception decode(String methodKey, Response response) {
|
|
public Exception decode(String methodKey, Response response) {
|
|
|
try {
|
|
try {
|
|
|
String rs = Util.toString(response.body().asReader());
|
|
String rs = Util.toString(response.body().asReader());
|
|
|
- log.error(rs);
|
|
|
|
|
- JSONObject result = JSONObject.parseObject(rs);
|
|
|
|
|
- return new BusinessException(result.getString("message"));
|
|
|
|
|
|
|
+ FeignProviderException feignProviderException = JSONObject.parseObject(rs, FeignProviderException.class);
|
|
|
|
|
+ log.info("rpc 远程调用返回错误:{}",rs);
|
|
|
|
|
+ if(feignProviderException.isBusinessException()) {
|
|
|
|
|
+ throw new BusinessException(feignProviderException.getCode(),feignProviderException.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ throw new Exception(feignProviderException.getMsg());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
catch (IOException ignored) {
|
|
catch (IOException ignored) {
|
|
|
|
|
+ log.error("rpc 返回数据异常",ignored);
|
|
|
return new BusinessException("数据异常");
|
|
return new BusinessException("数据异常");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|