2021-04-20

1.LcdpExceptionHandlerAdvice

 /*** 全局异常拦截
 */
@Slf4j
@ControllerAdvice
public class LcdpExceptionHandlerAdvice {
/**
 * Lcdp全局异常
 * @param request
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = LcdpException.class)
public BaseResult verifyErrorHandler(HttpServletRequest request, LcdpException e) {
    log.error(e.getMessage(), e);
    BaseResult result = new BaseResult();
    result.setCode(e.getErrorCode());
    result.setMessage(e.getErrorMessage());
    //堆栈信息
    result.setStackMsg(getStackTrace(e));
    return result;
}

/**
 * RuntimeException未捕获异常
 * @param request
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = RuntimeException.class)
public BaseResult verifyErrorHandler(HttpServletRequest request, RuntimeException e) {
    log.error(e.getMessage(), e);
    BaseResult baseResult = new BaseResult();
    baseResult.setCode(LcdpMessageEnum.E99999.getCode());
    baseResult.setMessage(LcdpMessageEnum.E99999.getMessage());
    //堆栈信息
    baseResult.setStackMsg(getStackTrace(e));
    return baseResult;
}

/**
 * Exception未捕获异常
 * @param request
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = Exception.class)
public BaseResult verifyErrorHandler(HttpServletRequest request, Exception e) {
    log.error(e.getMessage(), e);
    BaseResult baseResult = new BaseResult();
    baseResult.setCode(LcdpMessageEnum.E99999.getCode());
    baseResult.setMessage(LcdpMessageEnum.E99999.getMessage());
    //堆栈信息
    baseResult.setStackMsg(getStackTrace(e));
    return baseResult;
}

/**
 * 兼容SpringAssert异常消息处理
 * @param request
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = IllegalArgumentException.class)
public BaseResult verifyErrorHandle(HttpServletRequest request, IllegalArgumentException e) {
    log.error(e.getMessage(), e);
    BaseResult result = new BaseResult();
    result.setCode("50000");
    result.setMessage(e.getMessage());
    //堆栈信息
    result.setStackMsg(getStackTrace(e));
    return result;
}

//HTTP 401
@ResponseBody
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
@ExceptionHandler(value = UnauthorizedException.class)
public BaseResult verifyErrorHandle(HttpServletRequest request, UnauthorizedException e) {
    log.error(e.getMessage(), e);
    BaseResult result = new BaseResult();
    result.setCode(LcdpMessageEnum.E401.getCode());
    result.setMessage(e.getMessage());
    //堆栈信息
    result.setStackMsg(getStackTrace(e));
    return result;
}

//HTTP 403
@ResponseBody
@ResponseStatus(value = HttpStatus.FORBIDDEN)
@ExceptionHandler(value = ForbiddenException.class)
public BaseResult verifyErrorHandle(HttpServletRequest request, ForbiddenException e) {
    log.error(e.getMessage(), e);
    BaseResult result = new BaseResult();
    result.setCode(LcdpMessageEnum.E403.getCode());
    result.setMessage(e.getMessage());
    //堆栈信息
    result.setStackMsg(getStackTrace(e));
    return result;
}

//HTTP 404
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(value = NotFoundException.class)
public BaseResult verifyErrorHandle(HttpServletRequest request, NotFoundException e) {
    log.error(e.getMessage(), e);
    BaseResult result = new BaseResult();
    result.setCode(LcdpMessageEnum.E404.getCode());
    result.setMessage(e.getMessage());
    //堆栈信息
    result.setStackMsg(getStackTrace(e));
    return result;
}

/**
 * 获取堆栈信息
 *
 * @param throwable
 * @return
 */
private String getStackTrace(Throwable throwable) {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    throwable.printStackTrace(new PrintWriter(buf, true));
    return buf.toString();
}

}

你可能感兴趣的:(2021-04-20)