rust-cnb 0.1.2

[DEPRECATED - use `cnb` crate instead] rust-cnb with Generated API client
Documentation
//! 错误处理

use std::fmt;

/// API 错误类型
#[derive(Debug)]
pub enum ApiError {
    /// HTTP 请求错误
    RequestError(reqwest::Error),
    /// HTTP 状态码错误
    HttpError(u16),
    /// URL 解析错误
    UrlError(url::ParseError),
    /// JSON 序列化/反序列化错误
    JsonError(serde_json::Error),
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ApiError::RequestError(e) => write!(f, "请求错误: {}", e),
            ApiError::HttpError(status) => write!(f, "HTTP 错误: {}", status),
            ApiError::UrlError(e) => write!(f, "URL 解析错误: {}", e),
            ApiError::JsonError(e) => write!(f, "JSON 错误: {}", e),
        }
    }
}

impl std::error::Error for ApiError {}

impl From<reqwest::Error> for ApiError {
    fn from(error: reqwest::Error) -> Self {
        ApiError::RequestError(error)
    }
}

impl From<url::ParseError> for ApiError {
    fn from(error: url::ParseError) -> Self {
        ApiError::UrlError(error)
    }
}

impl From<serde_json::Error> for ApiError {
    fn from(error: serde_json::Error) -> Self {
        ApiError::JsonError(error)
    }
}

/// 结果类型别名
pub type Result<T> = std::result::Result<T, ApiError>;