Skip to content

Commit

Permalink
refactor: 将全局异常处理器未知异常的异常类型从 Exception 调整为 Throwable
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Aug 15, 2023
1 parent 487fa82 commit 90e1c64
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,6 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

/**
* 拦截未知的系统异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public R handleException(Exception e, HttpServletRequest request) {
log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e);
LogContextHolder.setException(e);
return R.fail(e.getMessage());
}

/**
* 拦截未知的运行时异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e);
LogContextHolder.setException(e);
return R.fail(e.getMessage());
}

/**
* 拦截业务异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ServiceException.class)
public R handleServiceException(ServiceException e, HttpServletRequest request) {
log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e);
LogContextHolder.setErrorMsg(e.getMessage());
return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}

/**
* 拦截自定义验证异常-错误请求
*/
Expand All @@ -104,25 +71,25 @@ public R handleBadRequestException(BadRequestException e, HttpServletRequest req
}

/**
* 拦截校验异常-绑定异常
* 拦截校验异常-违反约束异常
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public R handleBindException(BindException e, HttpServletRequest request) {
@ExceptionHandler(ConstraintViolationException.class)
public R constraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
String errorMsg = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ",");
String errorMsg = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ",");
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

/**
* 拦截校验异常-违反约束异常
* 拦截校验异常-绑定异常
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public R constraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
@ExceptionHandler(BindException.class)
public R handleBindException(BindException e, HttpServletRequest request) {
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
String errorMsg = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ",");
String errorMsg = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ",");
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}
Expand Down Expand Up @@ -153,17 +120,6 @@ public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchExc
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

/**
* 拦截校验异常-请求方式不支持异常
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
LogContextHolder.setErrorMsg(e.getMessage());
log.error("请求地址 [{}],不支持 [{}] 请求", request.getRequestURI(), e.getMethod());
return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage());
}

/**
* 拦截文件上传异常-超过上传大小限制
*/
Expand Down Expand Up @@ -219,4 +175,48 @@ public R handleNotRoleException(NotRoleException e, HttpServletRequest request)
log.error("请求地址 [{}],角色权限校验失败。", request.getRequestURI(), e);
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
}

/**
* 拦截校验异常-请求方式不支持异常
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
LogContextHolder.setErrorMsg(e.getMessage());
log.error("请求地址 [{}],不支持 [{}] 请求", request.getRequestURI(), e.getMethod());
return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage());
}

/**
* 拦截业务异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ServiceException.class)
public R handleServiceException(ServiceException e, HttpServletRequest request) {
log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e);
LogContextHolder.setErrorMsg(e.getMessage());
return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}

/**
* 拦截未知的运行时异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e);
LogContextHolder.setException(e);
return R.fail(e.getMessage());
}

/**
* 拦截未知的系统异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Throwable.class)
public R handleException(Throwable e, HttpServletRequest request) {
log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e);
LogContextHolder.setException(e);
return R.fail(e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ public class LogContext implements Serializable {
/**
* 异常信息
*/
private Exception exception;
private Throwable exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public static void remove() {
* @param e
* 异常信息
*/
public static void setException(Exception e) {
public static void setException(Throwable e) {
LogContext logContext = get();
if (logContext != null) {
if (null != logContext) {
logContext.setErrorMsg(e.getMessage());
logContext.setException(e);
}
Expand All @@ -80,7 +80,7 @@ public static void setException(Exception e) {
*/
public static void setErrorMsg(String errorMsg) {
LogContext logContext = get();
if (logContext != null) {
if (null != logContext) {
logContext.setErrorMsg(errorMsg);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private LogDO logElapsedTimeAndException() {
logDO.setErrorMsg(errorMsg);
}
// 记录异常详情
Exception exception = logContext.getException();
Throwable exception = logContext.getException();
if (null != exception) {
logDO.setStatus(LogStatusEnum.FAILURE);
logDO.setExceptionDetail(ExceptionUtil.stacktraceToString(exception, -1));
Expand Down

0 comments on commit 90e1c64

Please sign in to comment.