Springboot -- 自定义异常,异常处理

定义异常类

java 复制代码
package com.shore.my_spring_demo.exception;

import com.shore.my_spring_demo.common.enums.ErrorEnums;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class UsersException extends RuntimeException {
    private int code;

    public UsersException() {
        super();
    }

    public UsersException(String message) {
        super(message);
    }

    public UsersException(String message, Throwable cause) {
        super(message, cause);
    }

    public UsersException(int code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public UsersException(ErrorEnums errorEnums, Throwable cause) {
        super(errorEnums.getValue(), cause);
        this.code = errorEnums.getCode();
    }

    public UsersException(ErrorEnums errorEnums) {
        super(errorEnums.getValue());
        this.code = errorEnums.getCode();
    }

    public UsersException(Throwable cause) {
        super(cause);
    }


}

定义错误码

java 复制代码
package com.shore.my_spring_demo.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ErrorEnums {
    UN_LOGIN(101, "用户未登录"),
    USER_NOT_FOUND(102, "用户未找到"),
    USER_ALREADY_EXIST(103, "用户已存在"),
    FAIL_VALIDATE(201, "token 校验失败"),
    FAIL_ENCODE(202, "密码校验失败"),

    UNKNOWN_ERROR(901, "未知异常"),

    ;

    private final int code;
    private final String value;
}

定义全局异常处理 GlobalExceptionHandler

java 复制代码
package com.shore.my_spring_demo.web.advice;

import com.shore.my_spring_demo.common.enums.ErrorEnums;
import com.shore.my_spring_demo.exception.UsersException;
import com.shore.my_spring_demo.web.domain.common.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UsersException.class)
    public ResponseEntity<ApiResponse<Void>> UsersExceptionHandler(UsersException exception) {
        log.info("errorCode: {}, errorMsg: {}", exception.getCode(), exception.getMessage());
        return ResponseEntity.badRequest().body(ApiResponse.error(exception.getCode(), exception.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResponse<Void>> generalExceptionHandler(Exception exception) {
        log.error("未捕获异常", exception);
        return ResponseEntity.badRequest().body(ApiResponse.error(ErrorEnums.UNKNOWN_ERROR.getCode(), ErrorEnums.UNKNOWN_ERROR.getValue()));
    }
}
相关推荐
RainbowSea12 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea12 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑16 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613516 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊18 小时前
Java学习第22天 - 云原生与容器化
java
渣哥19 小时前
原来 Java 里线程安全集合有这么多种
java
间彧19 小时前
Spring Boot集成Spring Security完整指南
java
间彧20 小时前
Spring Secutiy基本原理及工作流程
java
Java水解21 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆1 天前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试