Skip to main content

linkrs_core/error/
manager.rs

1//! Manager Error Type
2//!
3//! Errors related to the Manager layer components, including the Schema Manager, Index Manager, and Storage Client.
4
5use thiserror::Error;
6
7use crate::error::codes::{ErrorCode, PublicError, ToPublicError};
8
9/// Manager operation result type
10pub type ManagerResult<T> = Result<T, ManagerError>;
11
12/// Incorrect classification of the error
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum ErrorCategory {
15    /// retryable error
16    Retryable,
17    /// An error that cannot be retried.
18    NonRetryable,
19}
20
21/// Manager Error Type
22#[derive(Error, Debug, Clone, PartialEq)]
23pub enum ManagerError {
24    #[error("Resource not found: {0}")]
25    NotFound(String),
26
27    #[error("Resource already exists: {0}")]
28    AlreadyExists(String),
29
30    #[error("Invalid input: {0}")]
31    InvalidInput(String),
32
33    #[error("Storage error: {0}")]
34    StorageError(String),
35
36    #[error("Schema error: {0}")]
37    SchemaError(String),
38
39    #[error("Transaction error: {0}")]
40    TransactionError(String),
41
42    #[error("Timeout error: {0}")]
43    TimeoutError(String),
44
45    #[error("Other error: {0}")]
46    Other(String),
47}
48
49impl ManagerError {
50    /// Obtaining the error classification
51    pub fn category(&self) -> ErrorCategory {
52        match self {
53            ManagerError::StorageError(_) | ManagerError::TimeoutError(_) => {
54                ErrorCategory::Retryable
55            }
56            _ => ErrorCategory::NonRetryable,
57        }
58    }
59
60    /// Checking for retryability
61    pub fn is_retryable(&self) -> bool {
62        matches!(self.category(), ErrorCategory::Retryable)
63    }
64
65    /// The error “Create not found” was not encountered during the process.
66    pub fn not_found(msg: impl Into<String>) -> Self {
67        Self::NotFound(msg.into())
68    }
69
70    /// Create Existing Error
71    pub fn already_exists(msg: impl Into<String>) -> Self {
72        Self::AlreadyExists(msg.into())
73    }
74
75    /// An error was generated due to invalid input.
76    pub fn invalid_input(msg: impl Into<String>) -> Self {
77        Self::InvalidInput(msg.into())
78    }
79
80    /// Creating Storage Errors
81    pub fn storage_error(msg: impl Into<String>) -> Self {
82        Self::StorageError(msg.into())
83    }
84
85    /// An error occurred while creating the schema.
86    pub fn schema_error(msg: impl Into<String>) -> Self {
87        Self::SchemaError(msg.into())
88    }
89
90    /// Create Transaction Error
91    pub fn transaction_error(msg: impl Into<String>) -> Self {
92        Self::TransactionError(msg.into())
93    }
94
95    /// An error occurred due to a timeout.
96    pub fn timeout_error(msg: impl Into<String>) -> Self {
97        Self::TimeoutError(msg.into())
98    }
99}
100
101impl ToPublicError for ManagerError {
102    fn to_public_error(&self) -> PublicError {
103        PublicError::new(self.to_error_code(), self.to_public_message())
104    }
105
106    fn to_error_code(&self) -> ErrorCode {
107        match self {
108            ManagerError::NotFound(_) => ErrorCode::ResourceNotFound,
109            ManagerError::AlreadyExists(_) => ErrorCode::ResourceAlreadyExists,
110            ManagerError::InvalidInput(_) => ErrorCode::InvalidInput,
111            ManagerError::TimeoutError(_) => ErrorCode::Timeout,
112            _ => ErrorCode::InternalError,
113        }
114    }
115
116    fn to_public_message(&self) -> String {
117        self.to_string()
118    }
119}