Commit a1641d5c authored by liaozan's avatar liaozan 🏀

Temporarily rollback exception handling

parent 06b29474
package com.schbrain.common.web.exception; 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.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.schbrain.common.constants.ResponseActionConstants; import com.schbrain.common.constants.ResponseActionConstants;
import com.schbrain.common.exception.BaseException; import com.schbrain.common.exception.BaseException;
import com.schbrain.common.web.result.ResponseDTO; 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 lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.*;
import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.bind.*;
import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.*;
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.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.support.MissingServletRequestPartException; import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException; 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 * @author liaozan
* @since 2019/10/14 * @since 2019/10/14
...@@ -74,7 +67,6 @@ public class GlobalExceptionHandler { ...@@ -74,7 +67,6 @@ public class GlobalExceptionHandler {
return loggingThenBuildResponse(ex, SERVER_ERROR); return loggingThenBuildResponse(ex, SERVER_ERROR);
} }
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(IllegalStateException.class) @ExceptionHandler(IllegalStateException.class)
public Object handleIllegalStateException(IllegalStateException ex) { public Object handleIllegalStateException(IllegalStateException ex) {
return loggingThenBuildResponse(ex, SERVER_ERROR); return loggingThenBuildResponse(ex, SERVER_ERROR);
...@@ -86,27 +78,23 @@ public class GlobalExceptionHandler { ...@@ -86,27 +78,23 @@ public class GlobalExceptionHandler {
return loggingThenBuildResponse(ex, PARAM_INVALID); return loggingThenBuildResponse(ex, PARAM_INVALID);
} }
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(AsyncRequestTimeoutException.class) @ExceptionHandler(AsyncRequestTimeoutException.class)
public Object handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex) { public Object handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex) {
return loggingThenBuildResponse(ex, SERVER_ERROR); return loggingThenBuildResponse(ex, SERVER_ERROR);
} }
/************************************* SQL Exception Handing *************************************/ /************************************* SQL Exception Handing *************************************/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(SQLException.class) @ExceptionHandler(SQLException.class)
public Object handleSQLException(SQLException ex) { public Object handleSQLException(SQLException ex) {
return loggingThenBuildResponse(ex, SERVER_ERROR); return loggingThenBuildResponse(ex, SERVER_ERROR);
} }
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(DataAccessException.class) @ExceptionHandler(DataAccessException.class)
public Object handleDataAccessException(DataAccessException ex) { public Object handleDataAccessException(DataAccessException ex) {
return loggingThenBuildResponse(ex, SERVER_ERROR); return loggingThenBuildResponse(ex, SERVER_ERROR);
} }
/************************************* Http Request Exception Handing *************************************/ /************************************* Http Request Exception Handing *************************************/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Object handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) { public Object handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
String errorMsg = StrUtil.format("不支持该HTTP方法: {}, 请使用 {}", ex.getMethod(), Arrays.toString(ex.getSupportedMethods())); String errorMsg = StrUtil.format("不支持该HTTP方法: {}, 请使用 {}", ex.getMethod(), Arrays.toString(ex.getSupportedMethods()));
...@@ -114,7 +102,6 @@ public class GlobalExceptionHandler { ...@@ -114,7 +102,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class) @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public Object handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex) { public Object handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex) {
String errorMsg = StrUtil.format("不支持该媒体类型: {}, 请使用 {}", ex.getContentType(), ex.getSupportedMediaTypes()); String errorMsg = StrUtil.format("不支持该媒体类型: {}, 请使用 {}", ex.getContentType(), ex.getSupportedMediaTypes());
...@@ -122,7 +109,6 @@ public class GlobalExceptionHandler { ...@@ -122,7 +109,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class) @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public Object handleHttpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException ex) { public Object handleHttpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException ex) {
String errorMsg = StrUtil.format("不支持的媒体类型, 请使用 {}", ex.getSupportedMediaTypes()); String errorMsg = StrUtil.format("不支持的媒体类型, 请使用 {}", ex.getSupportedMediaTypes());
...@@ -131,7 +117,6 @@ public class GlobalExceptionHandler { ...@@ -131,7 +117,6 @@ public class GlobalExceptionHandler {
} }
/************************************* Method Parameter Exception Handing *************************************/ /************************************* Method Parameter Exception Handing *************************************/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class) @ExceptionHandler(HttpMessageNotReadableException.class)
public Object handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) { public Object handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
String errorMsg = StrUtil.format("参数解析失败, {}", ex.getMessage()); String errorMsg = StrUtil.format("参数解析失败, {}", ex.getMessage());
...@@ -139,7 +124,6 @@ public class GlobalExceptionHandler { ...@@ -139,7 +124,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentTypeMismatchException.class) @ExceptionHandler(MethodArgumentTypeMismatchException.class)
public Object handlerMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { public Object handlerMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
Object value = ex.getValue(); Object value = ex.getValue();
...@@ -152,7 +136,6 @@ public class GlobalExceptionHandler { ...@@ -152,7 +136,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingPathVariableException.class) @ExceptionHandler(MissingPathVariableException.class)
public Object handleMissingPathVariableException(MissingPathVariableException ex) { public Object handleMissingPathVariableException(MissingPathVariableException ex) {
String errorMsg = StrUtil.format("丢失路径参数, 参数名: {}, 参数类型: {}", ex.getVariableName(), ex.getParameter().getParameterType()); String errorMsg = StrUtil.format("丢失路径参数, 参数名: {}, 参数类型: {}", ex.getVariableName(), ex.getParameter().getParameterType());
...@@ -160,7 +143,6 @@ public class GlobalExceptionHandler { ...@@ -160,7 +143,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingRequestCookieException.class) @ExceptionHandler(MissingRequestCookieException.class)
public Object handleMissingRequestCookieException(MissingRequestCookieException ex) { public Object handleMissingRequestCookieException(MissingRequestCookieException ex) {
String errorMsg = StrUtil.format("丢失Cookie参数, 参数名: {}, 参数类型: {}", ex.getCookieName(), ex.getParameter().getParameterType()); String errorMsg = StrUtil.format("丢失Cookie参数, 参数名: {}, 参数类型: {}", ex.getCookieName(), ex.getParameter().getParameterType());
...@@ -168,7 +150,6 @@ public class GlobalExceptionHandler { ...@@ -168,7 +150,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingRequestHeaderException.class) @ExceptionHandler(MissingRequestHeaderException.class)
public Object handleMissingRequestHeaderException(MissingRequestHeaderException ex) { public Object handleMissingRequestHeaderException(MissingRequestHeaderException ex) {
String errorMsg = StrUtil.format("丢失Header参数, 参数名: {}, 参数类型: {}", ex.getHeaderName(), ex.getParameter().getParameterType()); String errorMsg = StrUtil.format("丢失Header参数, 参数名: {}, 参数类型: {}", ex.getHeaderName(), ex.getParameter().getParameterType());
...@@ -176,7 +157,6 @@ public class GlobalExceptionHandler { ...@@ -176,7 +157,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestPartException.class) @ExceptionHandler(MissingServletRequestPartException.class)
public Object handleMissingServletRequestPartException(MissingServletRequestPartException ex) { public Object handleMissingServletRequestPartException(MissingServletRequestPartException ex) {
String errorMsg = StrUtil.format("丢失参数: {}", ex.getRequestPartName()); String errorMsg = StrUtil.format("丢失参数: {}", ex.getRequestPartName());
...@@ -184,7 +164,6 @@ public class GlobalExceptionHandler { ...@@ -184,7 +164,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class) @ExceptionHandler(MissingServletRequestParameterException.class)
public Object handleServletRequestParameterException(MissingServletRequestParameterException ex) { public Object handleServletRequestParameterException(MissingServletRequestParameterException ex) {
String errorMsg = StrUtil.format("丢失Query参数, 参数名: {}, 参数类型: {}", ex.getParameterName(), ex.getParameterType()); String errorMsg = StrUtil.format("丢失Query参数, 参数名: {}, 参数类型: {}", ex.getParameterName(), ex.getParameterType());
...@@ -192,7 +171,6 @@ public class GlobalExceptionHandler { ...@@ -192,7 +171,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ServletRequestBindingException.class) @ExceptionHandler(ServletRequestBindingException.class)
public Object handleServletRequestBindingException(ServletRequestBindingException ex) { public Object handleServletRequestBindingException(ServletRequestBindingException ex) {
String errorMsg = StrUtil.format("参数绑定失败: {}", ex.getMessage()); String errorMsg = StrUtil.format("参数绑定失败: {}", ex.getMessage());
...@@ -201,7 +179,6 @@ public class GlobalExceptionHandler { ...@@ -201,7 +179,6 @@ public class GlobalExceptionHandler {
} }
/************************************* Parameter Binding Exception Handing *************************************/ /************************************* Parameter Binding Exception Handing *************************************/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class) @ExceptionHandler(BindException.class)
public Object handleBindException(BindException ex) { public Object handleBindException(BindException ex) {
String errorMsg = buildBindingErrorMsg(ex.getBindingResult()); String errorMsg = buildBindingErrorMsg(ex.getBindingResult());
...@@ -209,7 +186,6 @@ public class GlobalExceptionHandler { ...@@ -209,7 +186,6 @@ public class GlobalExceptionHandler {
return buildResponse(ex, PARAM_INVALID, errorMsg); return buildResponse(ex, PARAM_INVALID, errorMsg);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class) @ExceptionHandler(ConstraintViolationException.class)
public Object handleConstraintViolationException(ConstraintViolationException ex) { public Object handleConstraintViolationException(ConstraintViolationException ex) {
String errorMsg = buildConstraintViolationErrorMsg(ex.getConstraintViolations()); String errorMsg = buildConstraintViolationErrorMsg(ex.getConstraintViolations());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment