|
|
@@ -1,31 +1,31 @@
|
|
|
+// base/src/main/java/com/base/exception/GlobalExceptionHandler.java
|
|
|
package com.base.exception;
|
|
|
+
|
|
|
+import com.base.utils.Result;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+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.bind.annotation.RestControllerAdvice;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-/**
|
|
|
- * @description 全局异常处理器
|
|
|
- * @author Mr.M
|
|
|
- * @date 2022/9/6 11:29
|
|
|
- * @version 1.0
|
|
|
- */
|
|
|
@Slf4j
|
|
|
-@RestControllerAdvice
|
|
|
+@RestControllerAdvice(basePackages = "com") // 确保能扫描到所有模块
|
|
|
public class GlobalExceptionHandler {
|
|
|
- //项目的自定义异常
|
|
|
- @ResponseBody //json返回
|
|
|
- @ExceptionHandler(WyMusicException.class) //XueChengPlusException.class异常
|
|
|
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)//返回异常码状态code500
|
|
|
- public RestErrorResponse customException(WyMusicException e) {
|
|
|
- log.error("【系统异常】{}",e.getErrMessage(),e);
|
|
|
- return new RestErrorResponse(e.getErrMessage());
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @ExceptionHandler(WyMusicException.class)
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ public Result<String> customException(WyMusicException e) {
|
|
|
+ log.error("【业务异常】{}", e.getErrMessage(), e);
|
|
|
+ return Result.fail(e.getErrMessage());
|
|
|
}
|
|
|
- //非自定义异常
|
|
|
+
|
|
|
@ResponseBody
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
- public RestErrorResponse exception(Exception e) {
|
|
|
- log.error("【系统异常】{}",e.getMessage(),e);
|
|
|
- throw new WyMusicException("自定义错误信息");
|
|
|
+ public Result<String> exception(Exception e) {
|
|
|
+ log.error("【系统异常】{}", e.getMessage(), e);
|
|
|
+ return Result.fail( e.getMessage());
|
|
|
}
|
|
|
-}
|
|
|
+}
|