Class GlobalExceptionHandler

java.lang.Object
io.github.xezzon.zeroweb.common.exception.GlobalExceptionHandler

@RestControllerAdvice public class GlobalExceptionHandler extends Object
全局异常处理,统一管理应用程序中抛出的各种异常,并将其转换为统一的 ErrorResult 响应格式。 通过 RestControllerAdvice 捕获所有控制器抛出的异常,并根据异常类型进行相应的处理, 包括业务异常、参数校验异常、认证授权异常以及其他未捕获的运行时异常。
Author:
xezzon
See Also:
  • Constructor Details

    • GlobalExceptionHandler

      public GlobalExceptionHandler()
  • Method Details

    • handleException

      @ExceptionHandler(ZerowebBusinessException.class) public org.springframework.http.ResponseEntity<ErrorResult> handleException(ZerowebBusinessException e, jakarta.servlet.http.HttpServletRequest request)
      处理 业务异常。 当业务逻辑中发生可预期的错误时,抛出此异常,并返回自定义的 HTTP 状态码和错误信息。
      Parameters:
      e - 业务异常实例
      request - 当前的 HTTP 请求
      Returns:
      包含错误详情的 ErrorResult 响应实体
    • handleException

      @ExceptionHandler(Throwable.class) public org.springframework.http.ResponseEntity<ErrorResult> handleException(Throwable e, jakarta.servlet.http.HttpServletRequest request)
      处理所有未被特定异常处理器捕获的 异常。 作为兜底的异常处理机制,捕获所有非业务异常,返回通用的服务器错误信息。
      Parameters:
      e - 异常实例
      request - 当前的 HTTP 请求
      Returns:
      包含错误详情的 ErrorResult 响应实体,HTTP 状态码为 500
    • handleException

      @ExceptionHandler(org.springframework.web.servlet.resource.NoResourceFoundException.class) public void handleException(org.springframework.web.servlet.resource.NoResourceFoundException e, jakarta.servlet.http.HttpServletRequest request) throws org.springframework.web.servlet.resource.NoResourceFoundException
      处理 请求资源不存在异常。 当请求的资源(如静态文件、API 访问点)不存在时,捕获此异常。 此方法会重新抛出异常,让 Spring Boot 默认的 BasicErrorController 或其他处理器接管。
      Parameters:
      e - 资源未找到异常实例
      request - 当前的 HTTP 请求
      Throws:
      org.springframework.web.servlet.resource.NoResourceFoundException - 重新抛出原始异常
    • handleException

      @ExceptionHandler(cn.dev33.satoken.exception.NotLoginException.class) public org.springframework.http.ResponseEntity<ErrorResult> handleException(cn.dev33.satoken.exception.NotLoginException e, jakarta.servlet.http.HttpServletRequest request)
      处理 未登录异常。 当用户未通过身份认证访问受保护资源时,捕获此异常。
      Parameters:
      e - 未登录异常实例
      request - 当前的 HTTP 请求
      Returns:
      包含错误详情的 ErrorResult 响应实体,HTTP 状态码为 401
    • handleForbiddenException

      @ExceptionHandler({cn.dev33.satoken.exception.NotRoleException.class,cn.dev33.satoken.exception.NotPermissionException.class}) public org.springframework.http.ResponseEntity<ErrorResult> handleForbiddenException(RuntimeException e, jakarta.servlet.http.HttpServletRequest request)
      处理 角色不足异常权限不足异常。 当用户已登录但没有足够的角色或权限访问特定资源时,捕获此异常。
      Parameters:
      e - 运行时异常实例 (NotRoleExceptionNotPermissionException)
      request - 当前的 HTTP 请求
      Returns:
      包含错误详情的 ErrorResult 响应实体,HTTP 状态码为 403
    • handleDataNotExistException

      @ExceptionHandler({jakarta.persistence.EntityNotFoundException.class,NoSuchElementException.class}) public org.springframework.http.ResponseEntity<ErrorResult> handleDataNotExistException(RuntimeException e, jakarta.servlet.http.HttpServletRequest request)
      处理 实体未找到异常元素不存在异常。 当尝试访问数据库中不存在的实体或集合中不存在的元素时,捕获此异常。
      Parameters:
      e - 运行时异常实例 (EntityNotFoundExceptionNoSuchElementException)
      request - 当前的 HTTP 请求
      Returns:
      包含错误详情的 ErrorResult 响应实体,HTTP 状态码为 400
    • handleException

      @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ResponseEntity<ErrorResult> handleException(org.springframework.web.bind.MethodArgumentNotValidException e, jakarta.servlet.http.HttpServletRequest request)
      处理 参数校验不通过异常。 当请求参数不符合校验规则时,捕获此异常,并返回详细的参数错误信息。
      Parameters:
      e - 参数校验异常实例
      request - 当前的 HTTP 请求
      Returns:
      包含参数错误详情的 ErrorResult 响应实体,HTTP 状态码为 400
    • handleException

      @ExceptionHandler(org.springframework.web.method.annotation.HandlerMethodValidationException.class) public org.springframework.http.ResponseEntity<ErrorResult> handleException(org.springframework.web.method.annotation.HandlerMethodValidationException e, jakarta.servlet.http.HttpServletRequest request)
      处理 方法参数验证异常。 当控制器方法的参数验证失败时,捕获此异常,并返回详细的参数错误信息。
      Parameters:
      e - 方法参数验证异常实例
      request - 当前的 HTTP 请求
      Returns:
      包含参数错误详情的 ErrorResult 响应实体,HTTP 状态码为 400
    • handleException

      @ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) public org.springframework.http.ResponseEntity<ErrorResult> handleException(org.springframework.web.HttpRequestMethodNotSupportedException e, jakarta.servlet.http.HttpServletRequest request)
      处理 HTTP 请求方法不支持异常。 当客户端使用了服务器不支持的 HTTP 方法(如 GET、POST 等)时,捕获此异常。
      Parameters:
      e - HTTP 请求方法不支持异常实例
      request - 当前的 HTTP 请求
      Returns:
      包含错误详情的 ErrorResult 响应实体,HTTP 状态码为 405