mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2025-01-04 01:52:21 +08:00
fix(GlobalExceptionHandler.java): 修改status返回方式,去除FeignException
This commit is contained in:
parent
f34c20ddaa
commit
a9c891e89f
@ -4,7 +4,6 @@ import cn.hutool.json.JSONObject;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.youlai.common.result.Result;
|
import com.youlai.common.result.Result;
|
||||||
import com.youlai.common.result.ResultCode;
|
import com.youlai.common.result.ResultCode;
|
||||||
import feign.FeignException;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.TypeMismatchException;
|
import org.springframework.beans.TypeMismatchException;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@ -13,13 +12,14 @@ import org.springframework.validation.BindException;
|
|||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import javax.validation.ConstraintViolationException;
|
import javax.validation.ConstraintViolationException;
|
||||||
|
import javax.validation.ValidationException;
|
||||||
import java.util.concurrent.CompletionException;
|
import java.util.concurrent.CompletionException;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@ -36,8 +36,9 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* 表单绑定到 java bean 出错时抛出 BindException 异常
|
* 表单绑定到 java bean 出错时抛出 BindException 异常
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(BindException.class)
|
@ExceptionHandler(BindException.class)
|
||||||
public <T> Result<T> processException(BindException e, HttpServletResponse response) {
|
public <T> Result<T> processException(BindException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
JSONObject msg = new JSONObject();
|
JSONObject msg = new JSONObject();
|
||||||
e.getAllErrors().forEach(error -> {
|
e.getAllErrors().forEach(error -> {
|
||||||
@ -50,15 +51,15 @@ public class GlobalExceptionHandler {
|
|||||||
error.getDefaultMessage());
|
error.getDefaultMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(ResultCode.PARAM_ERROR, msg.toString());
|
return Result.failed(ResultCode.PARAM_ERROR, msg.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 普通参数(非 java bean)校验出错时抛出 ConstraintViolationException 异常
|
* 普通参数(非 java bean)校验出错时抛出 ConstraintViolationException 异常
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(ConstraintViolationException.class)
|
@ExceptionHandler(ConstraintViolationException.class)
|
||||||
public <T> Result<T> processException(ConstraintViolationException e, HttpServletResponse response) {
|
public <T> Result<T> processException(ConstraintViolationException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
JSONObject msg = new JSONObject();
|
JSONObject msg = new JSONObject();
|
||||||
e.getConstraintViolations().forEach(constraintViolation -> {
|
e.getConstraintViolations().forEach(constraintViolation -> {
|
||||||
@ -66,126 +67,122 @@ public class GlobalExceptionHandler {
|
|||||||
String path = constraintViolation.getPropertyPath().toString();
|
String path = constraintViolation.getPropertyPath().toString();
|
||||||
msg.set(path, template);
|
msg.set(path, template);
|
||||||
});
|
});
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(ResultCode.PARAM_ERROR, msg.toString());
|
return Result.failed(ResultCode.PARAM_ERROR, msg.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
@ExceptionHandler(ValidationException.class)
|
||||||
|
public <T> Result<T> processException(ValidationException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return Result.failed(ResultCode.PARAM_ERROR, "参数校验失败");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NoHandlerFoundException
|
* NoHandlerFoundException
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||||
@ExceptionHandler(NoHandlerFoundException.class)
|
@ExceptionHandler(NoHandlerFoundException.class)
|
||||||
public <T> Result<T> processException(NoHandlerFoundException e, HttpServletResponse response) {
|
public <T> Result<T> processException(NoHandlerFoundException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.NOT_FOUND.value());
|
|
||||||
return Result.failed(ResultCode.RESOURCE_NOT_FOUND);
|
return Result.failed(ResultCode.RESOURCE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MissingServletRequestParameterException
|
* MissingServletRequestParameterException
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
||||||
public <T> Result<T> processException(MissingServletRequestParameterException e, HttpServletResponse response) {
|
public <T> Result<T> processException(MissingServletRequestParameterException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(ResultCode.PARAM_IS_NULL);
|
return Result.failed(ResultCode.PARAM_IS_NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MethodArgumentTypeMismatchException
|
* MethodArgumentTypeMismatchException
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||||
public <T> Result<T> processException(MethodArgumentTypeMismatchException e, HttpServletResponse response) {
|
public <T> Result<T> processException(MethodArgumentTypeMismatchException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(ResultCode.PARAM_ERROR, "类型错误");
|
return Result.failed(ResultCode.PARAM_ERROR, "类型错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ServletException
|
* ServletException
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(ServletException.class)
|
@ExceptionHandler(ServletException.class)
|
||||||
public <T> Result<T> processException(ServletException e, HttpServletResponse response) {
|
public <T> Result<T> processException(ServletException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(e.getMessage());
|
return Result.failed(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(IllegalArgumentException.class)
|
@ExceptionHandler(IllegalArgumentException.class)
|
||||||
public <T> Result<T> handleIllegalArgumentException(IllegalArgumentException e, HttpServletResponse response) {
|
public <T> Result<T> handleIllegalArgumentException(IllegalArgumentException e) {
|
||||||
log.error("非法参数异常,异常原因:{}", e.getMessage(), e);
|
log.error("非法参数异常,异常原因:{}", e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(e.getMessage());
|
return Result.failed(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(JsonProcessingException.class)
|
@ExceptionHandler(JsonProcessingException.class)
|
||||||
public <T> Result<T> handleJsonProcessingException(JsonProcessingException e, HttpServletResponse response) {
|
public <T> Result<T> handleJsonProcessingException(JsonProcessingException e) {
|
||||||
log.error("Json转换异常,异常原因:{}", e.getMessage(), e);
|
log.error("Json转换异常,异常原因:{}", e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(e.getMessage());
|
return Result.failed(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HttpMessageNotReadableException
|
* HttpMessageNotReadableException
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||||
public <T> Result<T> processException(HttpMessageNotReadableException e, HttpServletResponse response) {
|
public <T> Result<T> processException(HttpMessageNotReadableException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
String errorMessage = "请求体不可为空";
|
String errorMessage = "请求体不可为空";
|
||||||
Throwable cause = e.getCause();
|
Throwable cause = e.getCause();
|
||||||
if (cause != null) {
|
if (cause != null) {
|
||||||
errorMessage = convertMessage(cause);
|
errorMessage = convertMessage(cause);
|
||||||
}
|
}
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(errorMessage);
|
return Result.failed(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TypeMismatchException
|
* TypeMismatchException
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(TypeMismatchException.class)
|
@ExceptionHandler(TypeMismatchException.class)
|
||||||
public <T> Result<T> processException(TypeMismatchException e, HttpServletResponse response) {
|
public <T> Result<T> processException(TypeMismatchException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed(e.getMessage());
|
return Result.failed(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* FeignException
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(FeignException.class)
|
|
||||||
public <T> Result<T> processException(FeignException e, HttpServletResponse response) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed("微服务调用异常");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CompletionException
|
* CompletionException
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(CompletionException.class)
|
@ExceptionHandler(CompletionException.class)
|
||||||
public <T> Result<T> processException(CompletionException e, HttpServletResponse response) {
|
public <T> Result<T> processException(CompletionException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
if (e.getMessage().startsWith("feign.FeignException")) {
|
if (e.getMessage().startsWith("feign.FeignException")) {
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed("微服务调用异常");
|
return Result.failed("微服务调用异常");
|
||||||
}
|
}
|
||||||
return handleException(e, response);
|
return handleException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(BizException.class)
|
@ExceptionHandler(BizException.class)
|
||||||
public <T> Result<T> handleBizException(BizException e, HttpServletResponse response) {
|
public <T> Result<T> handleBizException(BizException e) {
|
||||||
log.error("业务异常,异常原因:{}", e.getMessage(), e);
|
log.error("业务异常,异常原因:{}", e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
if (e.getResultCode() != null) {
|
if (e.getResultCode() != null) {
|
||||||
return Result.failed(e.getResultCode());
|
return Result.failed(e.getResultCode());
|
||||||
}
|
}
|
||||||
return Result.failed(e.getMessage());
|
return Result.failed(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public <T> Result<T> handleException(Exception e, HttpServletResponse response) {
|
public <T> Result<T> handleException(Exception e) {
|
||||||
log.error("未知异常,异常原因:{}", e.getMessage(), e);
|
log.error("未知异常,异常原因:{}", e.getMessage(), e);
|
||||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
||||||
return Result.failed();
|
return Result.failed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user