linkrs_core/error/
manager.rs1use thiserror::Error;
6
7use crate::error::codes::{ErrorCode, PublicError, ToPublicError};
8
9pub type ManagerResult<T> = Result<T, ManagerError>;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum ErrorCategory {
15 Retryable,
17 NonRetryable,
19}
20
21#[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 pub fn category(&self) -> ErrorCategory {
52 match self {
53 ManagerError::StorageError(_) | ManagerError::TimeoutError(_) => {
54 ErrorCategory::Retryable
55 }
56 _ => ErrorCategory::NonRetryable,
57 }
58 }
59
60 pub fn is_retryable(&self) -> bool {
62 matches!(self.category(), ErrorCategory::Retryable)
63 }
64
65 pub fn not_found(msg: impl Into<String>) -> Self {
67 Self::NotFound(msg.into())
68 }
69
70 pub fn already_exists(msg: impl Into<String>) -> Self {
72 Self::AlreadyExists(msg.into())
73 }
74
75 pub fn invalid_input(msg: impl Into<String>) -> Self {
77 Self::InvalidInput(msg.into())
78 }
79
80 pub fn storage_error(msg: impl Into<String>) -> Self {
82 Self::StorageError(msg.into())
83 }
84
85 pub fn schema_error(msg: impl Into<String>) -> Self {
87 Self::SchemaError(msg.into())
88 }
89
90 pub fn transaction_error(msg: impl Into<String>) -> Self {
92 Self::TransactionError(msg.into())
93 }
94
95 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}