diff --git a/common/service-util/src/main/java/com/atguigu/exception/BunnyException.java b/common/service-util/src/main/java/com/atguigu/exception/BunnyException.java new file mode 100644 index 0000000..f92006b --- /dev/null +++ b/common/service-util/src/main/java/com/atguigu/exception/BunnyException.java @@ -0,0 +1,33 @@ +package com.atguigu.exception; + +import com.atguigu.common.result.ResultCodeEnum; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; +import lombok.extern.slf4j.Slf4j; + +@NoArgsConstructor +@Getter +@ToString +@Slf4j +public class BunnyException extends RuntimeException { + Integer code;// 状态码 + String message;// 描述信息 + + public BunnyException(Integer code, String message) { + super(message); + this.code = code; + this.message = message; + } + + public BunnyException(String message) { + super(message); + this.message = message; + } + + public BunnyException(ResultCodeEnum codeEnum) { + super(codeEnum.getMessage()); + this.code = codeEnum.getCode(); + this.message = codeEnum.getMessage(); + } +} diff --git a/common/service-util/src/main/java/com/atguigu/exception/GlobalExceptionHandler.java b/common/service-util/src/main/java/com/atguigu/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..7fbe80f --- /dev/null +++ b/common/service-util/src/main/java/com/atguigu/exception/GlobalExceptionHandler.java @@ -0,0 +1,82 @@ +package com.atguigu.exception; + + +import com.atguigu.common.result.Result; +import com.atguigu.common.result.ResultCodeEnum; +import com.atguigu.constant.MessageConstant; +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.RestControllerAdvice; + +import java.io.FileNotFoundException; +import java.nio.file.AccessDeniedException; +import java.sql.SQLIntegrityConstraintViolationException; + +@RestControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + // 自定义异常信息 + @ExceptionHandler(BunnyException.class) + @ResponseBody + public Result exceptionHandler(BunnyException exception) { + log.error("GlobalExceptionHandler===>自定义异常信息:{}", exception.getMessage()); + + Integer code = exception.getCode() != null ? exception.getCode() : 500; + return Result.error(null, code, exception.getMessage()); + } + + // 运行时异常信息 + @ExceptionHandler(RuntimeException.class) + @ResponseBody + public Result exceptionHandler(RuntimeException exception) throws FileNotFoundException { + log.error("GlobalExceptionHandler===>运行时异常信息:{}", exception.getMessage()); + exception.printStackTrace(); + return Result.error(null, 500, "出错了啦"); + } + + // 捕获系统异常 + @ExceptionHandler(Exception.class) + @ResponseBody + public Result error(Exception exception) { + log.error("GlobalExceptionHandler===>系统异常信息:{}", exception.getMessage()); + + return Result.error(null, 500, "系统异常"); + } + + // 特定异常处理 + @ExceptionHandler(ArithmeticException.class) + @ResponseBody + public Result error(ArithmeticException exception) { + log.error("GlobalExceptionHandler===>特定异常信息:{}", exception.getMessage()); + + return Result.error(null, 500, exception.getMessage()); + } + + // spring security异常 + @ExceptionHandler(AccessDeniedException.class) + @ResponseBody + public Result error(AccessDeniedException exception) throws AccessDeniedException { + log.error("GlobalExceptionHandler===>spring security异常:{}", exception.getMessage()); + + return Result.error(ResultCodeEnum.PERMISSION); + } + + // 处理SQL异常 + @ExceptionHandler(SQLIntegrityConstraintViolationException.class) + @ResponseBody + public Result exceptionHandler(SQLIntegrityConstraintViolationException exception) { + log.error("GlobalExceptionHandler===>处理SQL异常:{}", exception.getMessage()); + + String message = exception.getMessage(); + if (message.contains("Duplicate entry")) { + // 截取用户名 + String username = message.split(" ")[2]; + // 错误信息 + String errorMessage = username + MessageConstant.ALREADY_EXISTS; + return Result.error(errorMessage); + } else { + return Result.error(MessageConstant.UNKNOWN_ERROR); + } + } +}