diff --git a/src/main/java/iet/ustb/sf/exception/GlobalExceptionHandler.java b/src/main/java/iet/ustb/sf/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..0d399d0 --- /dev/null +++ b/src/main/java/iet/ustb/sf/exception/GlobalExceptionHandler.java @@ -0,0 +1,29 @@ +package iet.ustb.sf.exception; + +import iet.ustb.sf.common.R; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 全局异常处理器 + * + * @author huangge1199 + * @since 2025/6/30 10:56:33 + */ +@RestControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + + @ExceptionHandler(MyException.class) + public R businessExceptionHandler(MyException e) { + log.error("BusinessException", e); + return R.fail(e.getCode(), e.getMessage()); + } + + @ExceptionHandler(RuntimeException.class) + public R businessExceptionHandler(RuntimeException e) { + log.error("RuntimeException", e); + return R.fail(ErrorCode.SYSTEM_ERROR, "系统错误"); + } +}