提交 90e1c64d 编写于 作者: 查尔斯-BUG万象集's avatar 查尔斯-BUG万象集

refactor: 将全局异常处理器未知异常的异常类型从 Exception 调整为 Throwable

上级 487fa823
......@@ -59,39 +59,6 @@ import top.charles7c.cnadmin.common.util.holder.LogContextHolder;
@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());
}
/**
* 拦截自定义验证异常-错误请求
*/
......@@ -104,25 +71,25 @@ public class GlobalExceptionHandler {
}
/**
* 拦截校验异常-绑定异常
* 拦截校验异常-违反约束异常
*/
@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);
}
......@@ -153,17 +120,6 @@ public class GlobalExceptionHandler {
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());
}
/**
* 拦截文件上传异常-超过上传大小限制
*/
......@@ -219,4 +175,48 @@ public class GlobalExceptionHandler {
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());
}
}
\ No newline at end of file
......@@ -50,5 +50,5 @@ public class LogContext implements Serializable {
/**
* 异常信息
*/
private Exception exception;
private Throwable exception;
}
......@@ -64,9 +64,9 @@ public class LogContextHolder {
* @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);
}
......@@ -80,7 +80,7 @@ public class LogContextHolder {
*/
public static void setErrorMsg(String errorMsg) {
LogContext logContext = get();
if (logContext != null) {
if (null != logContext) {
logContext.setErrorMsg(errorMsg);
}
}
......
......@@ -141,7 +141,7 @@ public class LogInterceptor implements HandlerInterceptor {
logDO.setErrorMsg(errorMsg);
}
// 记录异常详情
Exception exception = logContext.getException();
Throwable exception = logContext.getException();
if (null != exception) {
logDO.setStatus(LogStatusEnum.FAILURE);
logDO.setExceptionDetail(ExceptionUtil.stacktraceToString(exception, -1));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册