focus/crates/focus-transport/src/error.rs

58 lines
1.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 传输层的错误类型。
//! Error type for the transport layer.
use std::fmt;
/// 传输层可能产生的错误。
/// Errors that can arise in the transport layer.
#[derive(Debug)]
pub enum TransportError {
/// URL 格式非法或不被支持。
/// The URL was malformed or unsupported.
BadUrl(String),
/// DNS 解析失败。
/// DNS resolution failed.
Dns(String),
/// 网络 I/O 错误TCP、TLS 或 HTTP
/// A network I/O error (TCP, TLS, or HTTP).
Io(String),
/// TLS 握手失败。
/// The TLS handshake failed.
Tls(String),
/// 服务器返回非 2xx 状态码或格式错误的响应。
/// The server returned a non-2xx status or a malformed response.
Http { status: u16, body: String },
/// 请求超时。
/// A timeout elapsed.
Timeout,
}
impl fmt::Display for TransportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TransportError::BadUrl(m) => write!(f, "bad url: {}", m),
TransportError::Dns(m) => write!(f, "dns error: {}", m),
TransportError::Io(m) => write!(f, "io error: {}", m),
TransportError::Tls(m) => write!(f, "tls error: {}", m),
TransportError::Http { status, body } => {
write!(f, "http error {}: {}", status, body)
}
TransportError::Timeout => write!(f, "request timed out"),
}
}
}
impl std::error::Error for TransportError {}
/// 将 std::io::Error 转换为传输层错误(归入 Io 变体)。
/// Convert a std::io::Error into a transport error (as the Io variant).
impl From<std::io::Error> for TransportError {
fn from(e: std::io::Error) -> Self {
TransportError::Io(e.to_string())
}
}
/// 传输层操作的统一返回类型别名。
/// Standard Result alias for transport operations.
pub type TransportResult<T> = Result<T, TransportError>;