全局异常处理器

自定义错误代码(枚举)

public enum ErrorCod {
    FAIL_TO_IMPORT,
    FAIL_TO_EXPORT,
    FAIL_TO_INSERT,
    FAIL_TO_UPDATE,
    FAIL_TO_DELETE,
    FAIL_TO_BATCH_OPERATE,
    ERROR_PARAM,
    UNHANDLED_EXCEPTION
}

自定义异常类

public class XxxException extends RuntimeException {
    private ErrorCod errorCod;

    public BizException(String errMsg,ErrorCod errorCod){
        super(errMsg);
        this.errorCod = errorCod;
    }

    public ErrorCod getErrorCod() {
        return errorCod;
    }

    public void setErrorCod(ErrorCod errorCod) {
        this.errorCod = errorCod;
    }
}

定义全局异常处理器

/**
 * 自定义异常处理器
 */
@RestController
@ControllerAdvice("com.xxx.yyy.controller")
public class RestExceptionHandler {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);
    private static final String ERROR_CODE = "errCode";
    private static final String ERROR_MESSAGE = "errMsg";
    private static final String PROMPT_MESSAGE_1 = "发生未处理异常: ";
    private static final String PROMPT_MESSAGE_2 = "发生异常原因为:";
    private static final String PROMPT_MESSAGE_3 = "异常堆栈信息为:";
    private static final String PROMPT_MESSAGE_4 = "未处理异常,请联系管理员:";
    private String errMessage;

    @ExceptionHandler(value = BizException.class)
    public ResponseEntity<Map<String, Object>> bizExceptionHandler(BizException e) {
        Map<String, Object> errResult = new HashMap<>();
        errResult.put(ERROR_CODE, e.getErrorCod());
        errResult.put(ERROR_MESSAGE, e.getMessage());
        return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<Map<String, Object>> commonExceptionHandler(Exception e) {
        errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
        LOGGER.error(errMessage);
        errMessage = PROMPT_MESSAGE_2 + e.getMessage();
        LOGGER.error(errMessage);
        errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
        LOGGER.error(errMessage);
        Map<String, Object> errResult = new HashMap<>();
        errResult.put(ERROR_CODE, ErrorCod.UNHANDLED_EXCEPTION);
        errResult.put(ERROR_MESSAGE, PROMPT_MESSAGE_4);
        return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(value = SQLException.class)
    public ResponseEntity<Map<String, Object>> sqlExceptionHandler(SQLException e) {
        while (null != e.getNextException()) {
            e = e.getNextException();
        }
        errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
        LOGGER.error(errMessage);
        errMessage = PROMPT_MESSAGE_2 + e.getMessage();
        LOGGER.error(errMessage);
        errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
        LOGGER.error(errMessage);
        Map<String, Object> errResult = new HashMap<>();
        errResult.put(ERROR_CODE, ErrorCod.UNHANDLED_EXCEPTION);
        errResult.put(ERROR_MESSAGE, PROMPT_MESSAGE_4);
        return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
    }


    @ExceptionHandler(value = InvocationTargetException.class)
    public ResponseEntity<Map<String, Object>> reflectExceptionHandler(InvocationTargetException e) {
        Throwable targetEx = e.getTargetException();
        Throwable t1 = e;
        if (null != targetEx) {
            while (null != t1.getCause()) {
                t1 = t1.getCause();
            }
            if (SQLException.class.isAssignableFrom(t1.getClass())) {
                SQLException sqle = (SQLException) t1;
                while (null != sqle.getNextException()) {
                    sqle = sqle.getNextException();
                }
                errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
                LOGGER.error(errMessage);
                errMessage = PROMPT_MESSAGE_2 + e.getMessage();
                LOGGER.error(errMessage);
                errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
                LOGGER.error(errMessage);
            } else {
                errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
                LOGGER.error(errMessage);
                errMessage = PROMPT_MESSAGE_2 + e.getMessage();
                LOGGER.error(errMessage);
                errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
                LOGGER.error(errMessage);
            }
        } else {
            errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
            LOGGER.error(errMessage);
            errMessage = PROMPT_MESSAGE_2 + e.getMessage();
            LOGGER.error(errMessage);
            errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
            LOGGER.error(errMessage);
        }
        Map<String, Object> errResult = new HashMap<>();
        errResult.put(ERROR_CODE, ErrorCod.UNHANDLED_EXCEPTION);
        errResult.put(ERROR_MESSAGE, PROMPT_MESSAGE_4);
        return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

像上文,poi那篇文章一样,有错误直接抛异常就可以了。每种解决方式都有优势、有弊端 没有最好的,只有最适合的,欢迎广大朋友提出优化,或更好的解决方案 大家共同进步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值