From 5ead7a8e3a128b9635b68c0fb38a0549ca2a3be3 Mon Sep 17 00:00:00 2001 From: liwu Date: Thu, 25 Jan 2024 14:55:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=AF=8F=E4=B8=AA=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=AF=B9=E5=BA=94=E7=9A=84http=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/exception/GlobalExceptionHandler.java | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/exception/GlobalExceptionHandler.java b/commons/web-common/src/main/java/com/schbrain/common/web/exception/GlobalExceptionHandler.java index 42f7204..05d0c17 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/exception/GlobalExceptionHandler.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/exception/GlobalExceptionHandler.java @@ -1,34 +1,41 @@ package com.schbrain.common.web.exception; +import static com.schbrain.common.constants.ResponseCodeConstants.PARAM_INVALID; +import static com.schbrain.common.constants.ResponseCodeConstants.SERVER_ERROR; +import static com.schbrain.common.util.support.ValidationMessageBuilder.buildBindingErrorMsg; +import static com.schbrain.common.util.support.ValidationMessageBuilder.buildConstraintViolationErrorMsg; + import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.util.StrUtil; import com.schbrain.common.constants.ResponseActionConstants; import com.schbrain.common.exception.BaseException; import com.schbrain.common.web.result.ResponseDTO; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; +import javax.validation.ConstraintViolationException; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataAccessException; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.util.ClassUtils; import org.springframework.validation.BindException; -import org.springframework.web.*; -import org.springframework.web.bind.*; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.HttpMediaTypeNotAcceptableException; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MissingPathVariableException; +import org.springframework.web.bind.MissingRequestCookieException; +import org.springframework.web.bind.MissingRequestHeaderException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.ServletRequestBindingException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.context.request.async.AsyncRequestTimeoutException; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.multipart.support.MissingServletRequestPartException; import org.springframework.web.servlet.NoHandlerFoundException; -import javax.validation.ConstraintViolationException; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; - -import static com.schbrain.common.constants.ResponseCodeConstants.PARAM_INVALID; -import static com.schbrain.common.constants.ResponseCodeConstants.SERVER_ERROR; -import static com.schbrain.common.util.support.ValidationMessageBuilder.buildBindingErrorMsg; -import static com.schbrain.common.util.support.ValidationMessageBuilder.buildConstraintViolationErrorMsg; - /** * @author liaozan * @since 2019/10/14 @@ -45,6 +52,7 @@ public class GlobalExceptionHandler { } /************************************* Base Exception Handing *************************************/ + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(BaseException.class) public Object handleBaseException(BaseException ex) { logError(ex); @@ -52,21 +60,25 @@ public class GlobalExceptionHandler { } /************************************* Common Exception Handing *************************************/ + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Throwable.class) public Object handleAll(Throwable ex) { return loggingThenBuildResponse(ex, SERVER_ERROR); } + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(NullPointerException.class) public Object handleNullPointerException(NullPointerException ex) { return loggingThenBuildResponse(ex, SERVER_ERROR); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(IllegalArgumentException.class) public Object handleIllegalArgumentException(IllegalArgumentException ex) { return loggingThenBuildResponse(ex, SERVER_ERROR); } + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(IllegalStateException.class) public Object handleIllegalStateException(IllegalStateException ex) { return loggingThenBuildResponse(ex, SERVER_ERROR); @@ -78,23 +90,27 @@ public class GlobalExceptionHandler { return loggingThenBuildResponse(ex, PARAM_INVALID); } + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(AsyncRequestTimeoutException.class) public Object handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex) { return loggingThenBuildResponse(ex, SERVER_ERROR); } /************************************* SQL Exception Handing *************************************/ + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(SQLException.class) public Object handleSQLException(SQLException ex) { return loggingThenBuildResponse(ex, SERVER_ERROR); } + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(DataAccessException.class) public Object handleDataAccessException(DataAccessException ex) { return loggingThenBuildResponse(ex, SERVER_ERROR); } /************************************* Http Request Exception Handing *************************************/ + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public Object handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) { String errorMsg = StrUtil.format("不支持该HTTP方法: {}, 请使用 {}", ex.getMethod(), Arrays.toString(ex.getSupportedMethods())); @@ -102,6 +118,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) @ExceptionHandler(HttpMediaTypeNotSupportedException.class) public Object handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex) { String errorMsg = StrUtil.format("不支持该媒体类型: {}, 请使用 {}", ex.getContentType(), ex.getSupportedMediaTypes()); @@ -109,6 +126,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.NOT_ACCEPTABLE) @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) public Object handleHttpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException ex) { String errorMsg = StrUtil.format("不支持的媒体类型, 请使用 {}", ex.getSupportedMediaTypes()); @@ -117,6 +135,7 @@ public class GlobalExceptionHandler { } /************************************* Method Parameter Exception Handing *************************************/ + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(HttpMessageNotReadableException.class) public Object handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) { String errorMsg = StrUtil.format("参数解析失败, {}", ex.getMessage()); @@ -124,6 +143,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentTypeMismatchException.class) public Object handlerMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { Object value = ex.getValue(); @@ -136,6 +156,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingPathVariableException.class) public Object handleMissingPathVariableException(MissingPathVariableException ex) { String errorMsg = StrUtil.format("丢失路径参数, 参数名: {}, 参数类型: {}", ex.getVariableName(), ex.getParameter().getParameterType()); @@ -143,6 +164,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingRequestCookieException.class) public Object handleMissingRequestCookieException(MissingRequestCookieException ex) { String errorMsg = StrUtil.format("丢失Cookie参数, 参数名: {}, 参数类型: {}", ex.getCookieName(), ex.getParameter().getParameterType()); @@ -150,6 +172,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingRequestHeaderException.class) public Object handleMissingRequestHeaderException(MissingRequestHeaderException ex) { String errorMsg = StrUtil.format("丢失Header参数, 参数名: {}, 参数类型: {}", ex.getHeaderName(), ex.getParameter().getParameterType()); @@ -157,6 +180,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingServletRequestPartException.class) public Object handleMissingServletRequestPartException(MissingServletRequestPartException ex) { String errorMsg = StrUtil.format("丢失参数: {}", ex.getRequestPartName()); @@ -164,6 +188,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingServletRequestParameterException.class) public Object handleServletRequestParameterException(MissingServletRequestParameterException ex) { String errorMsg = StrUtil.format("丢失Query参数, 参数名: {}, 参数类型: {}", ex.getParameterName(), ex.getParameterType()); @@ -171,6 +196,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(ServletRequestBindingException.class) public Object handleServletRequestBindingException(ServletRequestBindingException ex) { String errorMsg = StrUtil.format("参数绑定失败: {}", ex.getMessage()); @@ -179,6 +205,7 @@ public class GlobalExceptionHandler { } /************************************* Parameter Binding Exception Handing *************************************/ + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(BindException.class) public Object handleBindException(BindException ex) { String errorMsg = buildBindingErrorMsg(ex.getBindingResult()); @@ -186,6 +213,7 @@ public class GlobalExceptionHandler { return buildResponse(ex, PARAM_INVALID, errorMsg); } + @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(ConstraintViolationException.class) public Object handleConstraintViolationException(ConstraintViolationException ex) { String errorMsg = buildConstraintViolationErrorMsg(ex.getConstraintViolations()); -- GitLab