Skip to main content

burncloud_auto_update/
error.rs

1//! 自动更新错误类型
2
3use std::fmt;
4
5/// 自动更新错误类型
6#[derive(Debug)]
7pub enum UpdateError {
8    /// 网络错误
9    Network(String),
10    /// GitHub API 错误
11    GitHub(String),
12    /// 版本解析错误
13    Version(String),
14    /// 文件系统错误
15    FileSystem(String),
16    /// 权限错误
17    Permission(String),
18    /// 配置错误
19    Configuration(String),
20    /// 其他错误
21    Other(String),
22    /// 未知错误
23    Unknown(String),
24}
25
26impl fmt::Display for UpdateError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            UpdateError::Network(msg) => write!(f, "网络错误: {}", msg),
30            UpdateError::GitHub(msg) => write!(f, "GitHub API 错误: {}", msg),
31            UpdateError::Version(msg) => write!(f, "版本解析错误: {}", msg),
32            UpdateError::FileSystem(msg) => write!(f, "文件系统错误: {}", msg),
33            UpdateError::Permission(msg) => write!(f, "权限错误: {}", msg),
34            UpdateError::Configuration(msg) => write!(f, "配置错误: {}", msg),
35            UpdateError::Other(msg) => write!(f, "其他错误: {}", msg),
36            UpdateError::Unknown(msg) => write!(f, "未知错误: {}", msg),
37        }
38    }
39}
40
41impl std::error::Error for UpdateError {}
42
43impl From<anyhow::Error> for UpdateError {
44    fn from(error: anyhow::Error) -> Self {
45        UpdateError::Unknown(error.to_string())
46    }
47}
48
49impl From<self_update::errors::Error> for UpdateError {
50    fn from(error: self_update::errors::Error) -> Self {
51        match error {
52            self_update::errors::Error::Network(_) => {
53                UpdateError::Network(error.to_string())
54            }
55            self_update::errors::Error::Release(_) => {
56                UpdateError::GitHub(error.to_string())
57            }
58            _ => UpdateError::Unknown(error.to_string()),
59        }
60    }
61}
62
63/// 自动更新结果类型
64pub type UpdateResult<T> = Result<T, UpdateError>;