异常处理

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

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

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

异常封装

异常实体

  1. @Data
  2. class ApiError {
  3. private Integer status;
  4. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  5. private LocalDateTime timestamp;
  6. private String message;
  7. private ApiError() {
  8. timestamp = LocalDateTime.now();
  9. }
  10. public ApiError(Integer status,String message) {
  11. this();
  12. this.status = status;
  13. this.message = message;
  14. }
  15. }

自定义异常

1、通用异常

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

  1. @Getter
  2. public class BadRequestException extends RuntimeException{
  3. private Integer status = BAD_REQUEST.value();
  4. public BadRequestException(String msg){
  5. super(msg);
  6. }
  7. public BadRequestException(HttpStatus status,String msg){
  8. super(msg);
  9. this.status = status.value();
  10. }
  11. }

2、实体相关异常

(1) 实体不存在: EntityNotFoundException

  1. import org.springframework.util.StringUtils;
  2. /**
  3. * @author Zheng Jie
  4. * @date 2018-11-23
  5. */
  6. public class EntityNotFoundException extends RuntimeException {
  7. public EntityNotFoundException(Class clazz, String field, String val) {
  8. super(EntityNotFoundException.generateMessage(clazz.getSimpleName(), field, val));
  9. }
  10. private static String generateMessage(String entity, String field, String val) {
  11. return StringUtils.capitalize(entity)
  12. + " with " + field + " "+ val + " does not exist";
  13. }
  14. }

(2) 实体已存在:EntityExistException

  1. import org.springframework.util.StringUtils;
  2. /**
  3. * @author Zheng Jie
  4. * @date 2018-11-23
  5. */
  6. public class EntityExistException extends RuntimeException {
  7. public EntityExistException(Class clazz, String field, String val) {
  8. super(EntityExistException.generateMessage(clazz.getSimpleName(), field, val));
  9. }
  10. private static String generateMessage(String entity, String field, String val) {
  11. return StringUtils.capitalize(entity)
  12. + " with " + field + " "+ val + " existed";
  13. }
  14. }

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

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

全局异常拦截

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

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

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

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

具体使用

  1. // 通用异常
  2. throw new BadRequestException("发生了异常");
  3. // 通用异常,使用自定义状态码
  4. throw new BadRequestException(HttpStatus.OK, "发送了异常");
  5. // 实体存在异常
  6. throw new EntityExistException(User.class, "email", "elunez@qq.com");
  7. // 实体不存在异常
  8. throw new EntityNotFoundException(User.class, "userName", "test");