Skip to main content

lingxia_update/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Clone, Error)]
4pub enum UpdateError {
5    #[error("invalid parameter: {0}")]
6    InvalidParameter(String),
7    #[error("unsupported operation: {0}")]
8    UnsupportedOperation(String),
9    #[error("resource not found: {0}")]
10    ResourceNotFound(String),
11    #[error("I/O error: {0}")]
12    Io(String),
13    #[error("runtime error: {0}")]
14    Runtime(String),
15}
16
17impl UpdateError {
18    pub fn invalid_parameter(detail: impl Into<String>) -> Self {
19        Self::InvalidParameter(detail.into())
20    }
21
22    pub fn unsupported(detail: impl Into<String>) -> Self {
23        Self::UnsupportedOperation(detail.into())
24    }
25
26    pub fn not_found(detail: impl Into<String>) -> Self {
27        Self::ResourceNotFound(detail.into())
28    }
29
30    pub fn io(detail: impl Into<String>) -> Self {
31        Self::Io(detail.into())
32    }
33
34    pub fn runtime(detail: impl Into<String>) -> Self {
35        Self::Runtime(detail.into())
36    }
37}
38
39impl From<std::io::Error> for UpdateError {
40    fn from(error: std::io::Error) -> Self {
41        Self::Io(error.to_string())
42    }
43}
44
45impl From<lingxia_provider::ProviderError> for UpdateError {
46    fn from(error: lingxia_provider::ProviderError) -> Self {
47        match error.code() {
48            lingxia_provider::ProviderErrorCode::InvalidRequest => {
49                Self::InvalidParameter(error.detail().to_string())
50            }
51            lingxia_provider::ProviderErrorCode::NotFound => {
52                Self::ResourceNotFound(error.detail().to_string())
53            }
54            lingxia_provider::ProviderErrorCode::PermissionDenied => {
55                Self::UnsupportedOperation(error.detail().to_string())
56            }
57            lingxia_provider::ProviderErrorCode::Network
58            | lingxia_provider::ProviderErrorCode::Timeout
59            | lingxia_provider::ProviderErrorCode::Server
60            | lingxia_provider::ProviderErrorCode::Internal => Self::Runtime(error.to_string()),
61        }
62    }
63}