async_translate/
error.rs

1//! 翻译错误类型定义
2
3use std::fmt;
4
5/// 翻译错误类型
6#[derive(Debug)]
7pub enum TranslationError {
8    /// 网络请求错误
9    NetworkError(reqwest::Error),
10    /// HTTP 状态错误
11    HttpError {
12        status: reqwest::StatusCode,
13        body: String,
14    },
15    /// 认证错误
16    AuthenticationError(String),
17    /// 超时错误
18    TimeoutError,
19    /// 重试次数耗尽(包含每次尝试的错误信息)
20    MaxRetriesExceeded {
21        attempts: u32,
22        errors: Vec<TranslationError>, // 记录每次重试的错误
23    },
24    /// 翻译服务返回的错误
25    ServiceError(String),
26    /// 配置错误
27    ConfigurationError(String),
28    /// 其他错误
29    Other(String),
30}
31
32impl TranslationError {
33    /// 判断错误是否可以重试
34    pub fn is_retryable(&self) -> bool {
35        match self {
36            TranslationError::NetworkError(_) => true,
37            TranslationError::HttpError { status, .. } => {
38                // 5xx 状态码通常是服务器端问题,可以重试
39                status.is_server_error()
40            }
41            TranslationError::TimeoutError => true,
42            // 其他错误类型,如认证、配置、服务错误等,通常不可重试
43            _ => false,
44        }
45    }
46}
47
48impl fmt::Display for TranslationError {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            TranslationError::NetworkError(e) => write!(f, "Network error: {}", e),
52            TranslationError::HttpError { status, body } => {
53                write!(f, "HTTP error {}: {}", status, body)
54            }
55            TranslationError::AuthenticationError(msg) => {
56                write!(f, "Authentication error: {}", msg)
57            }
58            TranslationError::TimeoutError => write!(f, "Request timeout"),
59            TranslationError::MaxRetriesExceeded { attempts, errors } => {
60                writeln!(f, "Max retries exceeded after {} attempts", attempts)?;
61                for (i, error) in errors.iter().enumerate() {
62                    writeln!(f, "  Attempt {}: {}", i + 1, error)?;
63                }
64                Ok(())
65            }
66            TranslationError::ServiceError(msg) => write!(f, "Service error: {}", msg),
67            TranslationError::ConfigurationError(msg) => write!(f, "Configuration error: {}", msg),
68            TranslationError::Other(msg) => write!(f, "Error: {}", msg),
69        }
70    }
71}
72
73impl std::error::Error for TranslationError {
74    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
75        match self {
76            TranslationError::NetworkError(e) => Some(e),
77            TranslationError::MaxRetriesExceeded { .. } => None,
78            _ => None,
79        }
80    }
81}
82
83// 为常见的错误类型实现转换
84impl From<reqwest::Error> for TranslationError {
85    fn from(error: reqwest::Error) -> Self {
86        if error.is_timeout() {
87            TranslationError::TimeoutError
88        } else if error.is_connect() {
89            TranslationError::NetworkError(error)
90        } else {
91            TranslationError::NetworkError(error)
92        }
93    }
94}
95
96impl From<serde_json::Error> for TranslationError {
97    fn from(error: serde_json::Error) -> Self {
98        TranslationError::ServiceError(format!("JSON parsing error: {}", error))
99    }
100}
101
102impl From<anyhow::Error> for TranslationError {
103    fn from(error: anyhow::Error) -> Self {
104        TranslationError::Other(error.to_string())
105    }
106}