@RestControllerAdvice(annotations = { RestController.class, Controller.class })
public class BaseExceptionHandler {
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* 请求参数类型错误异常的捕获
*
* @param e
* @return
*/
@ExceptionHandler(value = { BindException.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public CommonResult<String> badRequest(BindException e) {
log.error("occurs error when execute method ,message {}", ExceptionUtils.getFullStackTrace(e));
return new CommonResult<>(ResponseErrorEnums.BAD_REQUEST);
}
/**
* 404错误异常的捕获
*
* @param e
* @return
*/
@ExceptionHandler(value = { NoHandlerFoundException.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public CommonResult<String> badRequestNotFound(BindException e) {
log.error("occurs error when execute method ,message {}", ExceptionUtils.getFullStackTrace(e));
return new CommonResult<>(ResponseErrorEnums.NOT_FOUND);
}
/**
* mybatis未绑定异常
*
* @param e
* @return
*/
@ExceptionHandler(BindingException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResult<String> mybatis(Exception e) {
log.error("occurs error when execute method ,message {}", ExceptionUtils.getFullStackTrace(e));
return new CommonResult<>(ResponseErrorEnums.BOUND_STATEMENT_NOT_FOUNT);
}
/**
* 自定义异常的捕获 自定义抛出异常。统一的在这里捕获返回JSON格式的友好提示。
*
* @param exception
* @param request
* @return
*/
@ExceptionHandler(value = { BaseException.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public <T extends Serializable> CommonResult<T> sendError(BaseException exception, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("occurs error when execute url ={} ,message {}", requestURI, ExceptionUtils.getFullStackTrace(exception));
return new CommonResult<>(exception.getCode(),
StringUtil.isNotEmpty(exception.getMessage()) ? exception.getMessage() : exception.getDetailMessage());
}
/**
* 数据库操作出现异常
*
* @param e
* @return
*/
@ExceptionHandler(value = { SQLException.class, DataAccessException.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResult<String> systemError(Exception e) {
log.error("occurs error when execute method ,message {}", ExceptionUtils.getFullStackTrace(e));
if(e instanceof MyBatisSystemException){
return new CommonResult<>(((MyBatisSystemException) e).getRootCause().getMessage());
}else{
CommonResult<String> commonResult = new CommonResult<>(ResponseErrorEnums.DATABASE_ERROR);
commonResult.setMessage(String.format("%s:%s", commonResult.getMessage(), ExceptionUtils.getRootCauseMessage(e)));
return commonResult;
}
}
/**
* 网络连接失败!
*
* @param e
* @return
*/
@ExceptionHandler(value = { ConnectException.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResult<String> connect(Exception e) {
log.error("occurs error when execute method ,message {}", ExceptionUtils.getFullStackTrace(e));
return new CommonResult<>(ResponseErrorEnums.CONNECTION_ERROR);
}
@ExceptionHandler(value = { RuntimeException.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResult<String> runTimeError(Exception e) {
log.error("occurs error when execute method ,message {}", ExceptionUtils.getFullStackTrace(e));
String errorMsg = ExceptionUtils.getRootCauseMessage(e);
String flowErrorMsg = ThreadMsgUtil.getMapMsg(ThreadMsgUtil.MSG_FLOW_ERROR, true);
if (StringUtil.isNotEmpty(errorMsg) && errorMsg.indexOf("流程异常") > -1 && StringUtil.isNotEmpty(flowErrorMsg)) {
errorMsg = flowErrorMsg;
}else if (StringUtil.isNotEmpty(errorMsg)) {
errorMsg = errorMsg.replace("RuntimeException:", "");
}
return new CommonResult<>(false, errorMsg);
}
@ExceptionHandler(value = { Exception.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResult<String> notAllowed(Exception e) {
log.error("occurs error when execute method ,message {}", ExceptionUtils.getFullStackTrace(e));
return new CommonResult<>(ResponseErrorEnums.SYSTEM_ERROR);
}
/**
* 校验错误信息收集
* @param e
* @return
*/
@ExceptionHandler(value = { MethodArgumentNotValidException.class })
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResult<String> MethodArgumentNotValidExceptiona(MethodArgumentNotValidException e) {
StringBuffer stringBuffer = new StringBuffer();
BindingResult bindingResult = e.getBindingResult();
bindingResult.getAllErrors().forEach(error-> stringBuffer.append(error.getDefaultMessage()+" "));
log.error("occurs error when execute method ,message {}", stringBuffer.toString());
return new CommonResult<String>(ResponseErrorEnums.ILLEGAL_ARGUMENT.getCode(),stringBuffer.toString());
}
}
SpringBoot异常处理
最新推荐文章于 2023-04-05 13:55:28 发布