异常处理
我们开发项目的时候,数据在请求过程中发生错误是非常常见的事情。
如:权限不足、数据唯一异常、数据不能为空异常、义务异常等。 这些异常如果不经过处理会对前端开发人员和使用者造成不便,因此我们就需要统一处理他们。
源码位于:源码位于:eladmin-common 模块中的 exception 包中
异常封装
异常实体
@Dataclass 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、通用异常
封装了 BadRequestException,用于处理通用的异常
@Getterpublic 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();}}
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";}}
(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";}}
使用场景,删除用户的时候是根据ID删除的,可判断ID是否存在,抛出异常
新增用户的时候用户名是唯一的,可判断用户是否存在,抛出异常
全局异常拦截
使用全局异常处理器 @RestControllerAdvice 处理请求发送的异常
@RestControllerAdvice:默认会扫描指定包中所有@RequestMapping注解
@ExceptionHandler:通过@ExceptionHandler的 value 属性可过滤拦截的条件
@RestControllerAdvicepublic 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()));}}
具体使用
// 通用异常throw new BadRequestException("发生了异常");// 通用异常,使用自定义状态码throw new BadRequestException(HttpStatus.OK, "发送了异常");// 实体存在异常throw new EntityExistException(User.class, "email", "elunez@qq.com");// 实体不存在异常throw new EntityNotFoundException(User.class, "userName", "test");
