谭斌 2 ani în urmă
părinte
comite
cebebb32fc

+ 36 - 0
device-api-cloud/src/main/java/com/xy/config/FeignErrorDecoder.java

@@ -0,0 +1,36 @@
+package com.xy.config;
+
+import com.alibaba.fastjson.JSONException;
+import com.alibaba.fastjson.JSONObject;
+import com.xy.error.CommRuntimeException;
+import feign.Response;
+import feign.Util;
+import feign.codec.ErrorDecoder;
+import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
+import org.springframework.context.annotation.Configuration;
+
+import java.io.IOException;
+
+@Configuration
+public class FeignErrorDecoder implements ErrorDecoder {
+
+    @Override
+    public Exception decode(final String methodKey, final Response response) {
+        try {
+            String message = Util.toString(response.body().asReader());
+            try {
+                JSONObject jsonObject = JSONObject.parseObject(message);
+                // 包装成自己自定义的异常,这里建议根据自己的代码改
+                return new CommRuntimeException(jsonObject.getString("message"));
+            } catch (JSONException e) {
+                e.printStackTrace();
+            }
+
+        } catch (IOException var4) {
+            return new InternalException(var4.getMessage());
+        }
+        return decode(methodKey, response);
+    }
+
+
+}