美文网首页
处理open feign的调用的[业务异常]透传至调用方

处理open feign的调用的[业务异常]透传至调用方

作者: 机灵鬼鬼 | 来源:发表于2021-08-26 15:48 被阅读0次

处理自定义异常,把后端服务业务异常传递给openfeign感知

package com.paraview.bum.user.api;

import com.paraview.bum.common.BusinessException;
import com.paraview.common.facade.common.Result;
import com.paraview.common.facade.common.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

/**
 * @author lius
 * @version v1.0
 * @date: 2021/8/26
 * @Description: 全局异常处理 (aop)
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class, ConstraintViolationException.class})
    @ResponseBody
    public Result handleMethodArgumentNotValidException(Exception e, HttpServletRequest request) {
        logger.debug("请求url:[{}]出错", request.getRequestURL(), e);
        StringBuilder errorMsg = new StringBuilder();
        BindingResult bindingResult = null;
        if (e instanceof BindException) {
            BindException bindException = (BindException) e;
            bindingResult = bindException.getBindingResult();
        }
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
            bindingResult = methodArgumentNotValidException.getBindingResult();
        }
        if (e instanceof ConstraintViolationException) {
            ConstraintViolationException constraintViolationException = (ConstraintViolationException) e;
            errorMsg.append(constraintViolationException.getMessage());
        }
        if (bindingResult != null) {
            for (FieldError filedError : bindingResult.getFieldErrors()) {
                if (errorMsg.length() > 0) {//这种写法比无脑的都新增逗号,然后剔除最后一个字段优雅,且不会出现数组下标越界的风险
                    errorMsg.append(",");
                }
                errorMsg.append(filedError.getDefaultMessage());
            }
        }
        return ResultUtils.error(errorMsg.toString());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleException(Exception e, HttpServletRequest request) {
        logger.warn("请求url:[{}]出错", request.getRequestURL(), e);
        return ResultUtils.error(e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result handleException(BusinessException e, HttpServletRequest request) {
        logger.warn("请求url:[{}]出错", request.getRequestURL(), e);
        return ResultUtils.error(e.getCode(),e.getMessage());
    }
}

相关文章

网友评论

      本文标题:处理open feign的调用的[业务异常]透传至调用方

      本文链接:https://www.haomeiwen.com/subject/upduiltx.html