处理feign decoder 的异常情况

import com.exception.BusinessException;
import com.exception.ErrorDetail;
import com.exception.GenericError;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class FeignErrorDecoder implements ErrorDecoder {
    private static final String SPLIT_CHAR = "#";

    @Autowired
    private ApplicationContext context;
    private Map<String, ErrorDecodeStrategy> strategyMap;

    @Override
    public Exception decode(String s, Response response) {
        String body = null;
        try {
            if (response.body() != null) {
                ErrorDecodeStrategy strategy = getDecoder(s);
                if (strategy != null) {
                    strategy.handleResponse(response);
                }
                body = Util.toString(response.body().asReader());
                log.error("调用外部服务异常,请求信息:{},异常返回:{}", response.request().toString(), body);
            }
        } catch (IOException e) {
            log.error("解析外部服务异常返回body失败,请求信息:{}", response.request().toString(), e);
        }
        return new BusinessException(new ErrorDetail(GenericError.BAD_REQUEST, "异常请求:" + response.request().toString() + "异常返回:" + body), response.status());
    }

    private ErrorDecodeStrategy getDecoder(String s) {
        if (strategyMap == null) {
            strategyMap = context.getBeansOfType(ErrorDecodeStrategy.class).values().stream().collect(Collectors.toMap(ErrorDecodeStrategy::getName, x -> x));
        }
        String clientClassName = s.split(SPLIT_CHAR)[0];
        return strategyMap.get(clientClassName);
    }

}
@Bean

@Primary

public ErrorDecoder errorDecoder() {    

return new FeignErrorDecoder();

}


注入到springboot容器

import feign.Response;
/**
 * Feign异常解码策略
 */
public interface ErrorDecodeStrategy {

    /**
     * 对应处理什么Client的如YstServiceClient
     *
     * @return
     */
    String getName();

    /**
     * 处理返回的结果
     *
     * @param response
     */
    void handleResponse(Response response);
}
import com.xception.BusinessException;
import com.exception.ErrorDetail;
import com.client.YServiceClient;
import com.client.dto.yst.YErrorResponse;
import com.exception.YErrorCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

import static com.exception.YErrorCode.ServerError;

/**
 * 移事通错误异常处理策略
 *
 */
@Service
@Slf4j
public class YstErrorDecodeStrategy implements ErrorDecodeStrategy {

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public String getName() {
        return YstServiceClient.class.getSimpleName();
    }

    @Override
    public void handleResponse(Response response) {
        try {
            YErrorResponse errorResponse = objectMapper.readValue(response.body().asReader(), YErrorResponse.class);
            YErrorCode yErrorCode = YErrorCode.valueOf(errorResponse.getCode());
            throw new BusinessException(new ErrorDetail(ystErrorCode, errorResponse.getMessage()), yErrorCode.getCode());
        } catch (IOException e) {
            log.error("yst异常返回解析失败,{}, {}", response, e.getMessage());
            throw new BusinessException(new ErrorDetail(ServerError, ServerError.getDescription()), ServerError.getCode());
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值