|
@@ -0,0 +1,36 @@
|
|
|
+package com.anyway.exception;
|
|
|
+
|
|
|
+import com.anyway.util.R;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.validation.ObjectError;
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: 全局异常处理器
|
|
|
+ *
|
|
|
+ * @author liuchuanwei
|
|
|
+ * @date 2024-08-13
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@ControllerAdvice
|
|
|
+public class GlobalExceptionHandler {
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public R<Void> handleValidException(MethodArgumentNotValidException e){
|
|
|
+ log.error("参数校验失败:{}", e.getMessage(), e);
|
|
|
+ String errorMessages = e.getBindingResult().getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(";"));
|
|
|
+ return R.fail(errorMessages);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ExceptionHandler(RuntimeException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public R<Void> handleRuntimeException(RuntimeException e){
|
|
|
+ log.error("系统异常:{}", e.getMessage(), e);
|
|
|
+ return R.fail("系统异常:" + e.getMessage());
|
|
|
+ }
|
|
|
+}
|