Spring Cloud(Webflux Feign)全局异常处理

自定义Exception

public class BusinessException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private int status = 200;
    private String code = "";
    private String trace = "";

    public BusinessException() {
        super();
    }

    public BusinessException(int status, String code, String message) {
        this(status, code, message, "");
    }

    public BusinessException(int status, String code, String message, String trace) {
        super(message);
        this.status = status;
        this.code = code;
        this.trace = trace;
    }

    public int getStatus() {
        return status;
    }

    public String getCode() {
        return code;
    }

    public String getTrace() {
        return trace;
    }
}

处理Exception的Handler

@Component
@Order(-2)
public class GlobalDefaultExceptionHandler extends AbstractErrorWebExceptionHandler {

    public GlobalDefaultExceptionHandler(ErrorAttributes g, ApplicationContext applicationContext,
            ServerCodecConfigurer serverCodecConfigurer) {
        super(g, new ResourceProperties(), applicationContext);
        super.setMessageWriters(serverCodecConfigurer.getWriters());
        super.setMessageReaders(serverCodecConfigurer.getReaders());
    }

    @Override
    protected RouterFunction getRoutingFunction(ErrorAttributes errorAttributes) {
        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
    }

    private Mono renderErrorResponse(ServerRequest request) {
        var errorPropertiesMap = getErrorAttributes(request, false);
        var status = (int) errorPropertiesMap.get("status");
        var exception = this.getError(request);
        var code = "";
        var message = "";

        if (exception instanceof BusinessException) {
            var businessException = (BusinessException) exception;

            status = businessException.getStatus();
            code = businessException.getCode();
            message = exception.getMessage();
        } else {
            code = String.valueOf(status);

            if (status == 400) {
                message = "参数错误";
            } else if (status == 401) {
                message = "无权访问";
            } else if (status == 404) {
                message = "未找到";
            } else if (status == 500) {
                message = "服务内部错误";
            }
        }

        var errorMap = new HashMap();

        errorMap.put("code", code);
        errorMap.put("message", message);

        return ServerResponse.status(HttpStatus.valueOf(status)).contentType(MediaType.APPLICATION_JSON_UTF8)
                .body(BodyInserters.fromObject(errorMap));
    }
}

Feign异常处理

@Component
public class ExceptionErrorDecoder implements ErrorDecoder {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public Exception decode(String methodKey, Response response) {
        BusinessException businessException = null;

        try {
            Map content = objectMapper.readValue(response.body().asInputStream(),
                    new TypeReference>() {
                    });

            businessException = new BusinessException(response.status(), content.getOrDefault("code", ""),
                    content.getOrDefault("message", ""), content.getOrDefault("trace", ""));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return businessException;
    }

}

你可能感兴趣的:(Spring Cloud(Webflux Feign)全局异常处理)