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
| @Slf4j @RestControllerAdvice public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.FORBIDDEN) @ExceptionHandler(value = AccessDeniedException.class) public Result<String> handler(AccessDeniedException e) { log.error("未授权异常:----------------{}", e.getMessage(), e); return ResultUtil.error(403, "用户未授权", e.getMessage()); }
@ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(value = NoHandlerFoundException.class) public Result<String> handler(NoHandlerFoundException e) { log.error("未找到资源:----------------{}", e.getMessage(), e); return ResultUtil.error(404, "未找到资源", e.getMessage()); }
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) @ExceptionHandler(value = HttpMediaTypeNotSupportedException.class) public Result<String> handler(HttpMediaTypeNotSupportedException e) { log.error("参数类型异常:----------------{}", e.getMessage(), e); return ResultUtil.error(415, "参数类型异常", e.getMessage()); }
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(value = BusinessException.class) public Result<String> handler(BusinessException e) { log.error("业务异常------------------{}", e.getErrorMessage(), e); return ResultUtil.error(e.getErrorCode(), "业务异常", e.getErrorMessage()); }
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(value = MethodArgumentNotValidException.class) public Result<String> handler(MethodArgumentNotValidException e) { log.error("实体校验异常:----------------{}", e.getMessage(), e); BindingResult bindingResult = e.getBindingResult(); ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get(); return ResultUtil.error("实体校验异常", objectError.getDefaultMessage()); }
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(value = IllegalArgumentException.class) public Result<String> handler(IllegalArgumentException e) { log.error("参数异常:----------------{}", e.getMessage(), e); return ResultUtil.error("参数异常", e.getMessage()); }
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(value = Exception.class) public Result<String> handler(Exception e) { log.error("服务器异常:----------------{}", e.getMessage(), e); return ResultUtil.error("服务器异常", e.getMessage()); }
}
|