ELADMIN 在线文档 ELADMIN 在线文档
  • 快速开始
  • 后端手册
  • 前端手册
  • 部署项目
常见问题
支持项目
特别鸣谢
主机推广
在线体验 (opens new window)
  • 快速开始
  • 后端手册
  • 前端手册
  • 部署项目
常见问题
支持项目
特别鸣谢
主机推广
在线体验 (opens new window)
  • 快速开始

    • 简介
    • 快速了解
    • 快速开始
  • 后端手册

    • 新增模块
    • 权限控制
    • 通用查询
    • 系统缓存
    • 异常处理
      • 异常处理
      • 异常封装
        • 异常实体
        • 自定义异常
        • 全局异常拦截
      • 具体使用
    • 系统日志
    • 数据权限
    • 定时任务
    • 代码生成
    • 运维管理
    • 系统工具
    • 其他杂项
  • 前端手册

    • 菜单路由
    • 自定义主键
    • 多字段排序
    • 隐藏操作按钮
    • 使用数据字典
    • 统一异常处理
    • 部分系统组件
  • 部署项目

    • 常规部署方式
    • 容器部署方式

异常处理

# 异常处理

我们开发项目的时候,数据在请求过程中发生错误是非常常见的事情。

如:权限不足、数据唯一异常、数据不能为空异常、业务异常等。 这些异常如果不经过处理会对前端开发人员和使用者造成不便,因此我们就需要统一处理他们。

源码位于:源码位于:eladmin-common 模块中的 exception 包中

# 异常封装

# 异常实体

@Data
class ApiError {
    private Integer status;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime timestamp;
    private String message;

    private ApiError() {
        timestamp = LocalDateTime.now();
    }

    public ApiError(Integer status,String message) {
        this();
        this.status = status;
        this.message = message;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 自定义异常

# 1、通用异常

封装了 BadRequestException,用于处理通用的异常

@Getter
public class BadRequestException extends RuntimeException{

    private Integer status = BAD_REQUEST.value();

    public BadRequestException(String msg){
        super(msg);
    }

    public BadRequestException(HttpStatus status,String msg){
        super(msg);
        this.status = status.value();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 2、实体相关异常

(1) 实体不存在: EntityNotFoundException

import org.springframework.util.StringUtils;

/**
 * @author Zheng Jie
 * @date 2018-11-23
 */
public class EntityNotFoundException extends RuntimeException {

    public EntityNotFoundException(Class clazz, String field, String val) {
        super(EntityNotFoundException.generateMessage(clazz.getSimpleName(), field, val));
    }

    private static String generateMessage(String entity, String field, String val) {
        return StringUtils.capitalize(entity)
                + " with " + field + " "+ val + " does not exist";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

(2) 实体已存在:EntityExistException

import org.springframework.util.StringUtils;

/**
 * @author Zheng Jie
 * @date 2018-11-23
 */
public class EntityExistException extends RuntimeException {

    public EntityExistException(Class clazz, String field, String val) {
        super(EntityExistException.generateMessage(clazz.getSimpleName(), field, val));
    }

    private static String generateMessage(String entity, String field, String val) {
        return StringUtils.capitalize(entity)
                + " with " + field + " "+ val + " existed";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

使用场景,删除用户的时候是根据ID删除的,可判断ID是否存在,抛出异常

新增用户的时候用户名是唯一的,可判断用户是否存在,抛出异常

# 全局异常拦截

使用全局异常处理器 @RestControllerAdvice 处理请求发送的异常

  • @RestControllerAdvice:默认会扫描指定包中所有@RequestMapping注解

  • @ExceptionHandler:通过@ExceptionHandler的 value 属性可过滤拦截的条件

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有不可知的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Throwable.class)
    public ResponseEntity handleException(Throwable e){
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        ApiError apiError = new ApiError(BAD_REQUEST.value(),e.getMessage());
        return buildResponseEntity(apiError);
    }
    /**
     * 处理自定义异常
     * @param e
     * @return
     */
	@ExceptionHandler(value = BadRequestException.class)
	public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        ApiError apiError = new ApiError(e.getStatus(),e.getMessage());
        return buildResponseEntity(apiError);
	}

    /**
     * 处理 EntityExist
     * @param e
     * @return
     */
    @ExceptionHandler(value = EntityExistException.class)
    public ResponseEntity<ApiError> entityExistException(EntityExistException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        ApiError apiError = new ApiError(BAD_REQUEST.value(),e.getMessage());
        return buildResponseEntity(apiError);
    }

    /**
     * 处理 EntityNotFound
     * @param e
     * @return
     */
    @ExceptionHandler(value = EntityNotFoundException.class)
    public ResponseEntity<ApiError> entityNotFoundException(EntityNotFoundException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        ApiError apiError = new ApiError(NOT_FOUND.value(),e.getMessage());
        return buildResponseEntity(apiError);
    }
    /**
     * 统一返回
     * @param apiError
     * @return
     */
    private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
        return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

# 具体使用

// 通用异常
throw new BadRequestException("发生了异常");
// 通用异常,使用自定义状态码
throw new BadRequestException(HttpStatus.OK, "发送了异常");
// 实体存在异常
throw new EntityExistException(User.class, "email", "elunez@qq.com");
// 实体不存在异常
 throw new EntityNotFoundException(User.class, "userName", "test");
1
2
3
4
5
6
7
8
帮助我们改善此页面! (opens new window)
上次更新: 2022/06/15, 15:15:51
系统缓存
系统日志

← 系统缓存 系统日志→

Theme by Vdoing | Copyright © 2018-2023 知了博客
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×