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