From f5a8ebf9f353f062ee48357fd58a7a5e067ccb2f Mon Sep 17 00:00:00 2001 From: many2many <6168830@qq.com> Date: Sun, 12 May 2024 17:00:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../paopao/exception/AuthFailedException.java | 7 +++ .../lk/paopao/exception/BaseException.java | 41 +++++++++++++++++ .../exception/GlobalExceptionHandler.java | 46 +++++++++++++++++++ .../exception/ResourceExistedException.java | 7 +++ .../exception/ResourceNotFoundException.java | 8 ++++ 5 files changed, 109 insertions(+) create mode 100644 src/main/java/com/lk/paopao/exception/AuthFailedException.java create mode 100644 src/main/java/com/lk/paopao/exception/BaseException.java create mode 100644 src/main/java/com/lk/paopao/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/lk/paopao/exception/ResourceExistedException.java create mode 100644 src/main/java/com/lk/paopao/exception/ResourceNotFoundException.java diff --git a/src/main/java/com/lk/paopao/exception/AuthFailedException.java b/src/main/java/com/lk/paopao/exception/AuthFailedException.java new file mode 100644 index 0000000..6633027 --- /dev/null +++ b/src/main/java/com/lk/paopao/exception/AuthFailedException.java @@ -0,0 +1,7 @@ +package com.lk.paopao.exception; + +public class AuthFailedException extends BaseException{ + public AuthFailedException(String message) { + super(401, message); + } +} diff --git a/src/main/java/com/lk/paopao/exception/BaseException.java b/src/main/java/com/lk/paopao/exception/BaseException.java new file mode 100644 index 0000000..3323d91 --- /dev/null +++ b/src/main/java/com/lk/paopao/exception/BaseException.java @@ -0,0 +1,41 @@ +package com.lk.paopao.exception; + +/** + * 定义一个基础异常类,用于封装业务异常信息。 + * 这个类是 RuntimeException 的子类,意味着它是一个非检查(Unchecked)异常。 + * 它添加了一个错误码(code)来详细描述异常的类型。 + */ +public abstract class BaseException extends RuntimeException{ + // 异常的错误码 + private final int code; + + /** + * 构造函数,用于创建一个带有错误码和消息的基础异常实例。 + * + * @param code 异常的错误码,用于标识异常的类型。 + * @param message 异常的详细信息,帮助理解异常的原因。 + */ + public BaseException(int code, String message){ + // 调用父类构造函数,将消息传递给父类,以初始化异常的详细信息。 + super(message); + this.code = code; + } + + /** + * 构造函数,用于创建一个带有错误码、消息和导致异常的原因的基础异常实例。 + * + * @param code 异常的错误码,用于标识异常的类型。 + * @param message 异常的详细信息,帮助理解异常的原因。 + * @param cause 导致异常的原因,便于问题追踪和处理。 + */ + public BaseException(int code, String message, Throwable cause) { + // 调用父类构造函数,将消息和原因传递给父类,以初始化异常的详细信息和原因。 + super(message, cause); + this.code = code; + } + + public int getCode() { + return code; + } + +} diff --git a/src/main/java/com/lk/paopao/exception/GlobalExceptionHandler.java b/src/main/java/com/lk/paopao/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..810874a --- /dev/null +++ b/src/main/java/com/lk/paopao/exception/GlobalExceptionHandler.java @@ -0,0 +1,46 @@ +package com.lk.paopao.exception; + +import com.lk.paopao.dto.rest.response.Result; +import com.lk.paopao.dto.rest.response.ResultUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * 全局异常处理器,负责处理Controller层抛出的异常,提供统一的异常响应格式。 + */ +@ControllerAdvice +public class GlobalExceptionHandler { + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + /** + * 处理自定义异常BaseException,返回相应的错误结果。 + * + * @param ex 自定义异常BaseException实例 + * @return 返回封装了错误信息的Result对象 + */ + @ExceptionHandler(BaseException.class) + @ResponseBody + public Result handleBaseException(BaseException ex) { + // 记录未预期异常信息 + log.info(ex.getMessage(),ex); + return ResultUtil.fail(ex.getCode(),ex.getMessage()); + } + + /** + * 处理未预期的异常,返回通用错误结果。 + * + * @param ex 异常实例 + * @return 返回封装了错误信息的Result对象 + */ + @ExceptionHandler(Exception.class) + @ResponseBody + public Result handleUnexpectedException(Exception ex) { + // 记录未预期异常信息 + log.warn(ex.getMessage(),ex); + // 返回通用错误结果, 500表示服务器内部错误 + return ResultUtil.fail(500,ex.getMessage()); + } +} diff --git a/src/main/java/com/lk/paopao/exception/ResourceExistedException.java b/src/main/java/com/lk/paopao/exception/ResourceExistedException.java new file mode 100644 index 0000000..5c2728b --- /dev/null +++ b/src/main/java/com/lk/paopao/exception/ResourceExistedException.java @@ -0,0 +1,7 @@ +package com.lk.paopao.exception; + +public class ResourceExistedException extends BaseException{ + public ResourceExistedException(String resourceName, String condition){ + super(409, "根据条件:【"+condition+"】可找到"+resourceName+"资源"); + } +} \ No newline at end of file diff --git a/src/main/java/com/lk/paopao/exception/ResourceNotFoundException.java b/src/main/java/com/lk/paopao/exception/ResourceNotFoundException.java new file mode 100644 index 0000000..31591cd --- /dev/null +++ b/src/main/java/com/lk/paopao/exception/ResourceNotFoundException.java @@ -0,0 +1,8 @@ +package com.lk.paopao.exception; + +public class ResourceNotFoundException extends BaseException{ + + public ResourceNotFoundException(String resourceName, String condition){ + super(404, "根据条件:【"+condition+"】无法找到"+resourceName+"资源"); + } +} \ No newline at end of file