torii_core/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum Error {
5    #[error("Authentication error: {0}")]
6    Auth(#[from] AuthError),
7
8    #[error("Storage error: {0}")]
9    Storage(#[from] StorageError),
10
11    #[error("Plugin error: {0}")]
12    Plugin(#[from] PluginError),
13
14    #[error("Validation error: {0}")]
15    Validation(#[from] ValidationError),
16
17    #[error("Event error: {0}")]
18    Event(#[from] EventError),
19
20    #[error("Session error: {0}")]
21    Session(#[from] SessionError),
22}
23
24#[derive(Debug, Error)]
25pub enum AuthError {
26    #[error("Invalid credentials")]
27    InvalidCredentials,
28
29    #[error("User not found")]
30    UserNotFound,
31
32    #[error("User already exists")]
33    UserAlreadyExists,
34
35    #[error("Email not verified")]
36    EmailNotVerified,
37
38    #[error("Unsupported authentication method: {0}")]
39    UnsupportedMethod(String),
40}
41
42#[derive(Debug, Error)]
43pub enum SessionError {
44    #[error("Session not found")]
45    NotFound,
46
47    #[error("Session expired")]
48    Expired,
49
50    #[error("Session already exists")]
51    AlreadyExists,
52
53    #[error("Invalid token: {0}")]
54    InvalidToken(String),
55
56    #[error("JWT verification failed: {0}")]
57    JwtVerification(String),
58}
59
60#[derive(Debug, Error)]
61pub enum StorageError {
62    #[error("Database error: {0}")]
63    Database(String),
64
65    #[error("Migration error: {0}")]
66    Migration(String),
67
68    #[error("Connection error: {0}")]
69    Connection(String),
70
71    #[error("Record not found")]
72    NotFound,
73}
74
75#[derive(Debug, Error)]
76pub enum PluginError {
77    #[error("Plugin not found: {0}")]
78    NotFound(String),
79
80    #[error("Plugin initialization failed: {0}")]
81    InitializationFailed(String),
82
83    #[error("Plugin type mismatch: {0}")]
84    TypeMismatch(String),
85
86    #[error("Plugin operation failed: {0}")]
87    OperationFailed(String),
88}
89
90#[derive(Debug, Error)]
91pub enum ValidationError {
92    #[error("Invalid email format")]
93    InvalidEmail,
94
95    #[error("Weak password")]
96    WeakPassword,
97
98    #[error("Invalid field: {0}")]
99    InvalidField(String),
100
101    #[error("Missing required field: {0}")]
102    MissingField(String),
103}
104
105#[derive(Debug, Error)]
106pub enum EventError {
107    #[error("Event bus error: {0}")]
108    BusError(String),
109
110    #[error("Event handler error: {0}")]
111    HandlerError(String),
112}
113
114impl Error {
115    pub fn is_auth_error(&self) -> bool {
116        matches!(
117            self,
118            Error::Auth(AuthError::InvalidCredentials)
119                | Error::Auth(AuthError::UserNotFound)
120                | Error::Auth(AuthError::UserAlreadyExists)
121        )
122    }
123
124    pub fn is_validation_error(&self) -> bool {
125        matches!(
126            self,
127            Error::Validation(ValidationError::InvalidEmail)
128                | Error::Validation(ValidationError::WeakPassword)
129                | Error::Validation(ValidationError::InvalidField(_))
130                | Error::Validation(ValidationError::MissingField(_))
131        )
132    }
133
134    pub fn is_plugin_error(&self) -> bool {
135        matches!(
136            self,
137            Error::Plugin(PluginError::NotFound(_))
138                | Error::Plugin(PluginError::TypeMismatch(_))
139                | Error::Plugin(PluginError::OperationFailed(_))
140        )
141    }
142}