清除无需要的
This commit is contained in:
parent
db95a112bc
commit
1d8a1f6eb9
@ -1,183 +0,0 @@
|
||||
package com.casic.machine.handler;
|
||||
|
||||
|
||||
import com.casic.machine.contants.CommonConstants;
|
||||
import com.casic.machine.enums.PermissionExceptionEnum;
|
||||
import com.casic.machine.enums.RequestExceptionEnum;
|
||||
import com.casic.machine.pojo.ErrorResponseData;
|
||||
import com.casic.machine.pojo.ResponseData;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import javax.naming.AuthenticationException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Order(-200)
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 请求参数异常
|
||||
*/
|
||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
||||
@ResponseBody
|
||||
public ResponseData handleMissingParameterException(MissingServletRequestParameterException e) {
|
||||
logger.error("请求参数异常{}", e.getMessage());
|
||||
return renderErrorJson(
|
||||
500,
|
||||
String.format("缺少请求参数{%s},类型为{%s}", e.getParameterName(), e.getParameterType()),
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截参数格式传递异常
|
||||
*/
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
@ResponseBody
|
||||
public ResponseData handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||
logger.error("参数格式传递异常{}",e.getMessage());
|
||||
return renderErrorJson(
|
||||
RequestExceptionEnum.REQUEST_JSON_ERROR.getCode(),
|
||||
RequestExceptionEnum.REQUEST_JSON_ERROR.getMessage(),
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截不支持媒体类型异常
|
||||
*/
|
||||
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
||||
@ResponseBody
|
||||
public ResponseData handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
|
||||
logger.error("不支持媒体类型异常{}",e.getMessage());
|
||||
return renderErrorJson(
|
||||
RequestExceptionEnum.REQUEST_TYPE_NOT_JSON.getCode(),
|
||||
RequestExceptionEnum.REQUEST_TYPE_NOT_JSON.getMessage(),
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截请求方法异常
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
@ResponseBody
|
||||
public ResponseData handleHttpRequestMethodNotSupportedException(HttpServletRequest httpServletRequest) {
|
||||
if ("GET".equalsIgnoreCase(httpServletRequest.getMethod())) {
|
||||
logger.error("请求方法异常{}",RequestExceptionEnum.REQUEST_METHOD_NOT_POST.getMessage());
|
||||
return renderErrorJson(
|
||||
RequestExceptionEnum.REQUEST_METHOD_NOT_POST.getCode(),
|
||||
RequestExceptionEnum.REQUEST_METHOD_NOT_POST.getMessage(),
|
||||
null
|
||||
);
|
||||
}
|
||||
if("POST".equalsIgnoreCase(httpServletRequest.getMethod())) {
|
||||
logger.error("请求方法异常{}",RequestExceptionEnum.REQUEST_METHOD_NOT_GET.getMessage());
|
||||
return renderErrorJson(
|
||||
RequestExceptionEnum.REQUEST_METHOD_NOT_GET.getCode(),
|
||||
RequestExceptionEnum.REQUEST_METHOD_NOT_GET.getMessage(),
|
||||
null
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截资源找不到的运行时异常
|
||||
*/
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
@ResponseBody
|
||||
public ResponseData handleNoHandlerFoundException(NoHandlerFoundException e) {
|
||||
logger.error("资源不存在异常{}",e.getMessage());
|
||||
return renderErrorJson(
|
||||
PermissionExceptionEnum.URL_NOT_EXIST.getCode(),
|
||||
PermissionExceptionEnum.NO_PERMISSION.getMessage(),
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截参数校验错误异常,JSON传参
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
@ResponseBody
|
||||
public ResponseData handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
logger.error("拦截参数校验错误异常,JSON传参{}",e.getMessage());
|
||||
return renderErrorJson(
|
||||
RequestExceptionEnum.PARAM_ERROR.getCode(),
|
||||
getArgNotValidMessage(e.getBindingResult()),
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截认证失败异常
|
||||
*/
|
||||
@ExceptionHandler(AuthenticationException.class)
|
||||
@ResponseBody
|
||||
public ResponseData handleAuthenticationException(AuthenticationException e) {
|
||||
logger.error("拦截认证失败异常{}",e.getMessage());
|
||||
return renderErrorJson(
|
||||
500,
|
||||
e.getMessage(),
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* 渲染异常Json
|
||||
*/
|
||||
private ErrorResponseData renderErrorJson(int code, String msg,Throwable e) {
|
||||
if (e != null) {
|
||||
StackTraceElement[] stackTrace = e.getStackTrace();
|
||||
|
||||
//默认的异常类全路径为第一条异常堆栈信息的
|
||||
String exceptionClassTotalName = stackTrace[0].toString();
|
||||
|
||||
//遍历所有堆栈信息,找到cd.casic开头的第一条异常信息
|
||||
for (StackTraceElement stackTraceElement : stackTrace) {
|
||||
if (stackTraceElement.toString().contains(CommonConstants.DEFAULT_PACKAGE_NAME)) {
|
||||
exceptionClassTotalName = stackTraceElement.toString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
ErrorResponseData error = ResponseData.error(code, msg);
|
||||
error.setExceptionClazz(exceptionClassTotalName);
|
||||
return error;
|
||||
}else {
|
||||
return ResponseData.error(code, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求参数不正确的提示信息
|
||||
* 多个信息,拼接成用逗号分隔的形式
|
||||
*/
|
||||
private String getArgNotValidMessage(BindingResult bindingResult) {
|
||||
if (bindingResult == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
//多个错误用逗号分隔
|
||||
return bindingResult.getAllErrors().stream()
|
||||
.map(ObjectError::getDefaultMessage)
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user