1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import cn.yunlongn.mall.common.core.exception.ServiceException; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import feign.Response; import feign.Util; import feign.codec.ErrorDecoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/** * 定义feign的异常处理类 在feign出异常的时候进入此类 * 例如feign的密码错误 */ @Configuration public class FeignErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) { try { // 这里直接拿到我们抛出的异常信息 String message = Util.toString(response.body().asReader()); try { JSONObject jsonObject = JSONObject.parseObject(message); return new ServiceException(jsonObject.getInteger("code"),jsonObject.getString("message")); } catch (JSONException e) { e.printStackTrace(); }
} catch (IOException ignored) {
} return decode(methodKey, response); } }
|