全局异常参数错误提示优化:部分异常日志输出堆栈减少

This commit is contained in:
gaoqr
2025-04-07 15:48:01 +08:00
parent 32e2bb9674
commit 669c276466
@@ -108,7 +108,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(value = MissingServletRequestParameterException.class) @ExceptionHandler(value = MissingServletRequestParameterException.class)
public CommonResult missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException ex) { public CommonResult missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException ex) {
log.warn("[missingServletRequestParameterExceptionHandler]", ex); log.warn(String.format("==========[missingServletRequestParameterExceptionHandler]==========%s", ex.getMessage()));
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数缺失:%s", ex.getParameterName())); return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数缺失:%s", ex.getParameterName()));
} }
@@ -119,7 +119,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(MethodArgumentTypeMismatchException.class) @ExceptionHandler(MethodArgumentTypeMismatchException.class)
public CommonResult methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) { public CommonResult methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
log.warn("[missingServletRequestParameterExceptionHandler]", ex); log.warn(String.format("==========[missingServletRequestParameterExceptionHandler]==========%s", ex.getMessage()));
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误,%s:%s", ex.getName(), ex.getValue())); return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误,%s:%s", ex.getName(), ex.getValue()));
} }
@@ -128,7 +128,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
public CommonResult methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException ex) { public CommonResult methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException ex) {
log.warn("[methodArgumentNotValidExceptionExceptionHandler]", ex); log.warn(String.format("==========[methodArgumentNotValidExceptionExceptionHandler]==========%s", ex.getMessage()));
FieldError fieldError = ex.getBindingResult().getFieldError(); FieldError fieldError = ex.getBindingResult().getFieldError();
assert fieldError != null; // 断言,避免告警 assert fieldError != null; // 断言,避免告警
return CommonResult.error(BAD_REQUEST.getCode(), fieldError.getDefaultMessage()); return CommonResult.error(BAD_REQUEST.getCode(), fieldError.getDefaultMessage());
@@ -139,7 +139,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(BindException.class) @ExceptionHandler(BindException.class)
public CommonResult bindExceptionHandler(BindException ex) { public CommonResult bindExceptionHandler(BindException ex) {
log.warn("[handleBindException]", ex); log.warn(String.format("==========[handleBindException]==========%s", ex.getMessage()));
FieldError fieldError = ex.getFieldError(); FieldError fieldError = ex.getFieldError();
assert fieldError != null; // 断言,避免告警 assert fieldError != null; // 断言,避免告警
String defaultMessage = fieldError.getDefaultMessage(); String defaultMessage = fieldError.getDefaultMessage();
@@ -156,7 +156,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(value = ConstraintViolationException.class) @ExceptionHandler(value = ConstraintViolationException.class)
public CommonResult constraintViolationExceptionHandler(ConstraintViolationException ex) { public CommonResult constraintViolationExceptionHandler(ConstraintViolationException ex) {
log.warn("[constraintViolationExceptionHandler]", ex); log.warn(String.format("==========[constraintViolationExceptionHandler]==========%s", ex.getMessage()));
ConstraintViolation constraintViolation = ex.getConstraintViolations().iterator().next(); ConstraintViolation constraintViolation = ex.getConstraintViolations().iterator().next();
String message = constraintViolation.getMessage(); String message = constraintViolation.getMessage();
if (StringUtils.isNotEmpty(message)) { if (StringUtils.isNotEmpty(message)) {
@@ -171,7 +171,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(value = ValidationException.class) @ExceptionHandler(value = ValidationException.class)
public CommonResult validationException(ValidationException ex) { public CommonResult validationException(ValidationException ex) {
log.warn("[constraintViolationExceptionHandler]", ex); log.warn(String.format("==========[constraintViolationExceptionHandler]==========%s", ex.getMessage()));
// 无法拼接明细的错误信息,因为 Dubbo Consumer 抛出 ValidationException 异常时,是直接的字符串信息,且人类不可读 // 无法拼接明细的错误信息,因为 Dubbo Consumer 抛出 ValidationException 异常时,是直接的字符串信息,且人类不可读
return CommonResult.error(BAD_REQUEST); return CommonResult.error(BAD_REQUEST);
} }
@@ -185,7 +185,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(NoHandlerFoundException.class) @ExceptionHandler(NoHandlerFoundException.class)
public CommonResult noHandlerFoundExceptionHandler(NoHandlerFoundException ex) { public CommonResult noHandlerFoundExceptionHandler(NoHandlerFoundException ex) {
log.warn("[noHandlerFoundExceptionHandler]", ex); log.warn(String.format("==========[noHandlerFoundExceptionHandler]==========%s", ex.getMessage()));
return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getRequestURL())); return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getRequestURL()));
} }
@@ -196,7 +196,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public CommonResult httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException ex) { public CommonResult httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException ex) {
log.warn("[httpRequestMethodNotSupportedExceptionHandler]", ex); log.warn(String.format("==========[httpRequestMethodNotSupportedExceptionHandler]==========%s", ex.getMessage()));
return CommonResult.error(METHOD_NOT_ALLOWED.getCode(), String.format("请求方法不正确:%s", ex.getMessage())); return CommonResult.error(METHOD_NOT_ALLOWED.getCode(), String.format("请求方法不正确:%s", ex.getMessage()));
} }
@@ -222,7 +222,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(value = HttpMessageNotReadableException.class) @ExceptionHandler(value = HttpMessageNotReadableException.class)
public CommonResult httpMessageNotReadableExceptionHandler(HttpServletRequest req, HttpMessageNotReadableException ex) { public CommonResult httpMessageNotReadableExceptionHandler(HttpServletRequest req, HttpMessageNotReadableException ex) {
log.warn("[httpMessageNotReadableExceptionHandler]", ex); log.warn(String.format("==========[httpMessageNotReadableExceptionHandler]==========%s", ex.getMessage()));
Throwable rootCause = ex.getRootCause(); Throwable rootCause = ex.getRootCause();
// 无效格式异常处理。 // 无效格式异常处理。
@@ -263,7 +263,7 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(value = DuplicateKeyException.class) @ExceptionHandler(value = DuplicateKeyException.class)
public CommonResult duplicateKeyExceptionHandler(DuplicateKeyException ex) { public CommonResult duplicateKeyExceptionHandler(DuplicateKeyException ex) {
log.info("[duplicateKeyExceptionHandler]", ex); log.warn(String.format("==========[duplicateKeyExceptionHandler]==========%s", ex.getMessage()));
return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), "请求数据已存在,请检查"); return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), "请求数据已存在,请检查");
} }