|
@@ -0,0 +1,88 @@
|
|
|
+package com.example.demo.exception;
|
|
|
+
|
|
|
+import com.example.demo.util.Result;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.validation.BindException;
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 全局异常捕获统一返回客户端
|
|
|
+ * @author Admin
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestControllerAdvice
|
|
|
+public class GlobalExceptionHandler {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理其他异常
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ExceptionHandler(value = Exception.class)
|
|
|
+ public Result exceptionHandler(Exception e){
|
|
|
+ log.error("程序错误原因:", e);
|
|
|
+ return Result.failure("服务端异常");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理其他异常
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ExceptionHandler(value = MissingServletRequestParameterException.class)
|
|
|
+ public Result exceptionHandler(MissingServletRequestParameterException e){
|
|
|
+ log.error("缺少必要参数:", e);
|
|
|
+ return Result.failure(HttpStatus.BAD_REQUEST.value(), "缺少必要参数");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常
|
|
|
+ */
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
+ public Result MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
|
|
|
+ String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
|
|
|
+ //下边ResultCodeEnum.PARAMS_BS_ERROR.getCode()就是你自己自定义的返回code码
|
|
|
+ return Result.failure(HttpStatus.BAD_REQUEST.value(), message);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常
|
|
|
+ */
|
|
|
+ @ExceptionHandler(BindException.class)
|
|
|
+ public Result BindExceptionHandler(BindException e) {
|
|
|
+ String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
|
|
|
+ return Result.failure(HttpStatus.BAD_REQUEST.value(), message);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @link https://www.cnblogs.com/cjyboy/p/11465876.html
|
|
|
+ * 处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是ConstraintViolationException
|
|
|
+ */
|
|
|
+// @ExceptionHandler(ConstraintViolationException.class)
|
|
|
+// public ViewResult ConstraintViolationExceptionHandler(ConstraintViolationException e) {
|
|
|
+// String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
|
|
|
+// return ViewResult.error(HttpStatus.BAD_REQUEST.value(), message);
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @ExceptionHandler(RuntimeException.class)
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public Result runtimeExceptionHandler(HttpServletRequest request, RuntimeException e) {
|
|
|
+ log.error("uri:{},stack trace:",request.getRequestURI(),e);
|
|
|
+ return Result.failure(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|