Class ExceptionConverter

java.lang.Object
cloud.opencode.base.web.exception.ExceptionConverter

public final class ExceptionConverter extends Object
Exception Converter 异常转换器

Converts exceptions to Result objects.

将异常转换为Result对象。

Features | 主要功能:

  • Exception to Result conversion - 异常到 Result 的转换
  • HTTP status mapping from exceptions - 从异常映射 HTTP 状态码
  • Support for OpenWebException, OpenBizException hierarchy - 支持 OpenWebException、OpenBizException 层次
  • Standard exception type handling (IllegalArgument, SecurityException, etc.) - 标准异常类型处理

Usage Examples | 使用示例:

// Convert exception to Result
Result<?> result = ExceptionConverter.toResult(exception);

// Get error code from exception
String code = ExceptionConverter.getCode(exception);

// Get HTTP status from exception
int status = ExceptionConverter.getHttpStatus(exception);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (handles null exceptions) - 空值安全: 是(处理 null 异常)

Performance | 性能特性:

  • Time complexity: O(1) - fixed number of instanceof checks regardless of input - 时间复杂度: O(1) - 固定数量的 instanceof 检查,与输入无关
  • Space complexity: O(1) - creates a single Result object per call - 空间复杂度: O(1) - 每次调用创建一个 Result 对象
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • toResult

      public static <T> Result<T> toResult(Throwable throwable)
      Convert exception to Result 将异常转换为Result
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      throwable - the exception | 异常
      Returns:
      the result | 结果
    • getCode

      public static String getCode(Throwable throwable)
      Get error code from exception 从异常获取错误代码
      Parameters:
      throwable - the exception | 异常
      Returns:
      the error code | 错误代码
    • getHttpStatus

      public static int getHttpStatus(Throwable throwable)
      Get HTTP status from exception 从异常获取HTTP状态
      Parameters:
      throwable - the exception | 异常
      Returns:
      the HTTP status | HTTP状态