Class OAuth2Exception

All Implemented Interfaces:
Serializable

public class OAuth2Exception extends OpenException
OAuth2 Exception OAuth2 异常

Base exception class for all OAuth2 related errors, extending OpenException for unified error handling across OpenCode components.

所有 OAuth2 相关错误的基础异常类,继承 OpenException 以实现 OpenCode 组件的统一错误处理。

Features | 主要功能:

  • Extends OpenException for unified error hierarchy - 继承 OpenException 实现统一异常层次
  • OAuth2 protocol exception with error code - 带错误码的 OAuth2 协议异常
  • Carries error description and URI - 携带错误描述和 URI
  • RFC 6749 server error response parsing - RFC 6749 服务器错误响应解析
  • Formatted message output [OAuth2] (Code) Message - 格式化消息输出

Usage Examples | 使用示例:

// Throw with error code
throw new OAuth2Exception(OAuth2ErrorCode.TOKEN_EXPIRED);

// Throw with error code and details
throw new OAuth2Exception(OAuth2ErrorCode.AUTHORIZATION_FAILED, "Invalid code");

// Throw with error code and cause
throw new OAuth2Exception(OAuth2ErrorCode.NETWORK_ERROR, e);

// Create from server error response (RFC 6749)
throw OAuth2Exception.fromServerError("invalid_grant", "The authorization code has expired", "https://example.com/errors/expired");

Security | 安全性:

  • Thread-safe: Yes (immutable exception) - 线程安全: 是(不可变异常)
  • Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)
Since:
JDK 25, opencode-base-oauth2 V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • OAuth2Exception

      public OAuth2Exception(OAuth2ErrorCode errorCode)
      Create exception with error code 使用错误码创建异常
      Parameters:
      errorCode - the error code | 错误码
    • OAuth2Exception

      public OAuth2Exception(OAuth2ErrorCode errorCode, String details)
      Create exception with error code and details 使用错误码和详情创建异常
      Parameters:
      errorCode - the error code | 错误码
      details - additional details | 附加详情
    • OAuth2Exception

      public OAuth2Exception(OAuth2ErrorCode errorCode, Throwable cause)
      Create exception with error code and cause 使用错误码和原因创建异常
      Parameters:
      errorCode - the error code | 错误码
      cause - the cause | 原因
    • OAuth2Exception

      public OAuth2Exception(OAuth2ErrorCode errorCode, String details, Throwable cause)
      Create exception with error code, details and cause 使用错误码、详情和原因创建异常
      Parameters:
      errorCode - the error code | 错误码
      details - additional details | 附加详情
      cause - the cause | 原因
    • OAuth2Exception

      public OAuth2Exception(OAuth2ErrorCode errorCode, String details, String serverError, String serverErrorDescription, String serverErrorUri)
      Create exception with error code and server error fields (RFC 6749) 使用错误码和服务器错误字段创建异常(RFC 6749)
      Parameters:
      errorCode - the error code | 错误码
      details - additional details | 附加详情
      serverError - the OAuth2 server error string | OAuth2 服务器错误字符串
      serverErrorDescription - the server error description | 服务器错误描述
      serverErrorUri - the server error URI | 服务器错误 URI
  • Method Details

    • errorCode

      public OAuth2ErrorCode errorCode()
      Get the error code 获取错误码
      Returns:
      the error code | 错误码
    • details

      public String details()
      Get additional details 获取附加详情
      Returns:
      the details or null | 详情或 null
    • code

      public int code()
      Get the error code number 获取错误码数字
      Returns:
      the error code number | 错误码数字
    • serverError

      public String serverError()
      Get the server error string from the OAuth2 error response (RFC 6749) 获取 OAuth2 错误响应中的服务器错误字符串(RFC 6749)
      Returns:
      the server error string or null | 服务器错误字符串或 null
    • serverErrorDescription

      public String serverErrorDescription()
      Get the server error description from the OAuth2 error response (RFC 6749) 获取 OAuth2 错误响应中的服务器错误描述(RFC 6749)
      Returns:
      the server error description or null | 服务器错误描述或 null
    • serverErrorUri

      public String serverErrorUri()
      Get the server error URI from the OAuth2 error response (RFC 6749) 获取 OAuth2 错误响应中的服务器错误 URI(RFC 6749)
      Returns:
      the server error URI or null | 服务器错误 URI 或 null
    • getMessage

      public String getMessage()
      Get the formatted exception message, including server error details when present. 获取格式化的异常消息,当存在时包含服务器错误详情。
      Overrides:
      getMessage in class OpenException
      Returns:
      the formatted message | 格式化的消息
    • tokenExpired

      public static OAuth2Exception tokenExpired()
      Create token expired exception 创建 Token 过期异常
      Returns:
      the exception | 异常
    • tokenInvalid

      public static OAuth2Exception tokenInvalid(String details)
      Create token invalid exception 创建 Token 无效异常
      Parameters:
      details - the details | 详情
      Returns:
      the exception | 异常
    • authorizationFailed

      public static OAuth2Exception authorizationFailed(String details)
      Create authorization failed exception 创建授权失败异常
      Parameters:
      details - the details | 详情
      Returns:
      the exception | 异常
    • networkError

      public static OAuth2Exception networkError(Throwable cause)
      Create network error exception 创建网络错误异常
      Parameters:
      cause - the cause | 原因
      Returns:
      the exception | 异常
    • invalidConfig

      public static OAuth2Exception invalidConfig(String details)
      Create invalid config exception 创建无效配置异常
      Parameters:
      details - the details | 详情
      Returns:
      the exception | 异常
    • fromServerError

      public static OAuth2Exception fromServerError(String error, String errorDescription, String errorUri)
      Create exception from an OAuth2 server error response (RFC 6749 Section 5.2) 从 OAuth2 服务器错误响应创建异常(RFC 6749 第 5.2 节)
      Parameters:
      error - the error code string from server | 服务器返回的错误码字符串
      errorDescription - the error description from server | 服务器返回的错误描述
      errorUri - the error URI from server | 服务器返回的错误 URI
      Returns:
      the exception | 异常