refactor: 根据阿里开发规范手册完善和调整错误码

This commit is contained in:
haoxr
2024-12-16 17:08:02 +08:00
parent fe163b7275
commit bd0f42f80b
11 changed files with 299 additions and 81 deletions

View File

@@ -30,85 +30,97 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* 全局系统异常处理器
* <p>
* 调整异常处理的HTTP状态码丰富异常处理类型
*
* @author Gadfly
* @since 2020-02-25 13:54
**/
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 处理绑定异常
* <p>
* 当请求参数绑定到对象时发生错误,会抛出 BindException 异常。
*/
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(BindException e) {
log.error("BindException:{}", e.getMessage());
String msg = e.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(""));
return Result.failed(ResultCode.PARAM_ERROR, msg);
return Result.failed(ResultCode.USER_REQUEST_PARAMETER_ERROR, msg);
}
/**
* RequestParam参数校验
*
* @param e
* @param <T>
* @return
* 处理 @RequestParam 参数校验异常
* <p>
* 当请求参数在校验过程中发生违反约束条件的异常时(如 @RequestParam 验证不通过),
* 会捕获到 ConstraintViolationException 异常。
*/
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(ConstraintViolationException e) {
log.error("ConstraintViolationException:{}", e.getMessage());
String msg = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(""));
return Result.failed(ResultCode.PARAM_ERROR, msg);
return Result.failed(ResultCode.INVALID_USER_INPUT, msg);
}
/**
* RequestBody参数校验
*
* @param e
* @param <T>
* @return
* 处理方法参数校验异常
* <p>
* 当使用 @Valid 或 @Validated 注解对方法参数进行验证时,如果验证失败,
* 会抛出 MethodArgumentNotValidException 异常。
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(MethodArgumentNotValidException e) {
log.error("MethodArgumentNotValidException:{}", e.getMessage());
String msg = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(""));
return Result.failed(ResultCode.PARAM_ERROR, msg);
return Result.failed(ResultCode.INVALID_USER_INPUT, msg);
}
/**
* 处理接口不存在的异常
* <p>
* 当客户端请求一个不存在的路径时,会抛出 NoHandlerFoundException 异常。
*/
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public <T> Result<T> processException(NoHandlerFoundException e) {
log.error(e.getMessage(), e);
return Result.failed(ResultCode.RESOURCE_NOT_FOUND);
return Result.failed(ResultCode.INTERFACE_NOT_EXIST);
}
/**
* MissingServletRequestParameterException
* 处理缺少请求参数的异常
* <p>
* 当请求缺少必需的参数时,会抛出 MissingServletRequestParameterException 异常。
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(MissingServletRequestParameterException e) {
log.error(e.getMessage(), e);
return Result.failed(ResultCode.PARAM_IS_NULL);
return Result.failed(ResultCode.REQUEST_REQUIRED_PARAMETER_IS_EMPTY);
}
/**
* MethodArgumentTypeMismatchException
* 处理方法参数类型不匹配的异常
* <p>
* 当请求参数类型不匹配时,会抛出 MethodArgumentTypeMismatchException 异常。
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(MethodArgumentTypeMismatchException e) {
log.error(e.getMessage(), e);
return Result.failed(ResultCode.PARAM_ERROR, "类型错误");
return Result.failed(ResultCode.PARAMETER_FORMAT_MISMATCH, "类型错误");
}
/**
* ServletException
* 处理 Servlet 异常
* <p>
* 当 Servlet 处理请求时发生异常时,会抛出 ServletException 异常。
*/
@ExceptionHandler(ServletException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@@ -117,6 +129,11 @@ public class GlobalExceptionHandler {
return Result.failed(e.getMessage());
}
/**
* 处理非法参数异常
* <p>
* 当方法接收到非法参数时,会抛出 IllegalArgumentException 异常。
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleIllegalArgumentException(IllegalArgumentException e) {
@@ -124,6 +141,11 @@ public class GlobalExceptionHandler {
return Result.failed(e.getMessage());
}
/**
* 处理 JSON 处理异常
* <p>
* 当处理 JSON 数据时发生错误,会抛出 JsonProcessingException 异常。
*/
@ExceptionHandler(JsonProcessingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleJsonProcessingException(JsonProcessingException e) {
@@ -132,7 +154,9 @@ public class GlobalExceptionHandler {
}
/**
* HttpMessageNotReadableException
* 处理请求体不可读的异常
* <p>
* 当请求体不可读时,会抛出 HttpMessageNotReadableException 异常。
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@@ -146,6 +170,11 @@ public class GlobalExceptionHandler {
return Result.failed(errorMessage);
}
/**
* 处理类型不匹配异常
* <p>
* 当方法参数类型不匹配时,会抛出 TypeMismatchException 异常。
*/
@ExceptionHandler(TypeMismatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(TypeMismatchException e) {
@@ -153,18 +182,28 @@ public class GlobalExceptionHandler {
return Result.failed(e.getMessage());
}
/**
* 处理 SQL 语法错误异常
* <p>
* 当 SQL 语法错误时,会抛出 BadSqlGrammarException 异常。
*/
@ExceptionHandler(BadSqlGrammarException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public <T> Result<T> handleBadSqlGrammarException(BadSqlGrammarException e) {
log.error(e.getMessage(), e);
String errorMsg = e.getMessage();
if (StrUtil.isNotBlank(errorMsg) && errorMsg.contains("denied to user")) {
return Result.failed(ResultCode.FORBIDDEN_OPERATION);
return Result.failed(ResultCode.ACCESS_UNAUTHORIZED);
} else {
return Result.failed(e.getMessage());
}
}
/**
* 处理 SQL 语法错误异常
* <p>
* 当 SQL 语法错误时,会抛出 SQLSyntaxErrorException 异常。
*/
@ExceptionHandler(SQLSyntaxErrorException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public <T> Result<T> processSQLSyntaxErrorException(SQLSyntaxErrorException e) {
@@ -172,7 +211,11 @@ public class GlobalExceptionHandler {
return Result.failed(e.getMessage());
}
/**
* 处理业务异常
* <p>
* 当业务逻辑发生错误时,会抛出 BusinessException 异常。
*/
@ExceptionHandler(BusinessException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleBizException(BusinessException e) {
@@ -183,9 +226,14 @@ public class GlobalExceptionHandler {
return Result.failed(e.getMessage());
}
/**
* 处理所有未捕获的异常
* <p>
* 当发生未捕获的异常时,会抛出 Exception 异常。
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleException(Exception e) throws Exception{
public <T> Result<T> handleException(Exception e) throws Exception {
// 将 Spring Security 异常继续抛出,以便交给自定义处理器处理
if (e instanceof AccessDeniedException
|| e instanceof AuthenticationException) {
@@ -216,4 +264,4 @@ public class GlobalExceptionHandler {
}
return group;
}
}
}