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, "系统错误"); + } +}