From eba78157d92009949538131c455acd2d8c9753d1 Mon Sep 17 00:00:00 2001 From: Wonder Date: Sat, 18 Oct 2025 14:35:38 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=84Update:=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../thumb/exception/BusinessException.java | 3 +++ .../exception/GlobalExceptionHandler.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/main/java/cn/hezhaohui/thumb/exception/GlobalExceptionHandler.java diff --git a/src/main/java/cn/hezhaohui/thumb/exception/BusinessException.java b/src/main/java/cn/hezhaohui/thumb/exception/BusinessException.java index 7ddcc91..7f48acd 100644 --- a/src/main/java/cn/hezhaohui/thumb/exception/BusinessException.java +++ b/src/main/java/cn/hezhaohui/thumb/exception/BusinessException.java @@ -1,5 +1,8 @@ package cn.hezhaohui.thumb.exception; +import lombok.Getter; + +@Getter public class BusinessException extends RuntimeException{ private final int code; diff --git a/src/main/java/cn/hezhaohui/thumb/exception/GlobalExceptionHandler.java b/src/main/java/cn/hezhaohui/thumb/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..3dd8fc5 --- /dev/null +++ b/src/main/java/cn/hezhaohui/thumb/exception/GlobalExceptionHandler.java @@ -0,0 +1,24 @@ +package cn.hezhaohui.thumb.exception; + +import cn.hezhaohui.thumb.common.BaseResponse; +import cn.hezhaohui.thumb.common.ResultUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + + @ExceptionHandler(BusinessException.class) + public BaseResponse businessExceptionHandler(BusinessException e) { + log.error("BusinessException", e); + return ResultUtils.error(e.getCode(), e.getMessage()); + } + + @ExceptionHandler(RuntimeException.class) + public BaseResponse runtimeExceptionHandler(RuntimeException e) { + log.error("RuntimeException", e); + return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误"); + } +}