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
54#[derive(Debug, Error)]
55pub enum StorageError {
56    #[error("Database error: {0}")]
57    Database(String),
58
59    #[error("Migration error: {0}")]
60    Migration(String),
61
62    #[error("Connection error: {0}")]
63    Connection(String),
64
65    #[error("Record not found")]
66    NotFound,
67}
68
69#[derive(Debug, Error)]
70pub enum PluginError {
71    #[error("Plugin not found: {0}")]
72    NotFound(String),
73
74    #[error("Plugin initialization failed: {0}")]
75    InitializationFailed(String),
76
77    #[error("Plugin type mismatch: {0}")]
78    TypeMismatch(String),
79
80    #[error("Plugin operation failed: {0}")]
81    OperationFailed(String),
82}
83
84#[derive(Debug, Error)]
85pub enum ValidationError {
86    #[error("Invalid email format")]
87    InvalidEmail,
88
89    #[error("Weak password")]
90    WeakPassword,
91
92    #[error("Invalid field: {0}")]
93    InvalidField(String),
94
95    #[error("Missing required field: {0}")]
96    MissingField(String),
97}
98
99#[derive(Debug, Error)]
100pub enum EventError {
101    #[error("Event bus error: {0}")]
102    BusError(String),
103
104    #[error("Event handler error: {0}")]
105    HandlerError(String),
106}
107
108impl Error {
109    pub fn is_auth_error(&self) -> bool {
110        matches!(
111            self,
112            Error::Auth(AuthError::InvalidCredentials)
113                | Error::Auth(AuthError::UserNotFound)
114                | Error::Auth(AuthError::UserAlreadyExists)
115        )
116    }
117
118    pub fn is_validation_error(&self) -> bool {
119        matches!(
120            self,
121            Error::Validation(ValidationError::InvalidEmail)
122                | Error::Validation(ValidationError::WeakPassword)
123                | Error::Validation(ValidationError::InvalidField(_))
124                | Error::Validation(ValidationError::MissingField(_))
125        )
126    }
127
128    pub fn is_plugin_error(&self) -> bool {
129        matches!(
130            self,
131            Error::Plugin(PluginError::NotFound(_))
132                | Error::Plugin(PluginError::TypeMismatch(_))
133                | Error::Plugin(PluginError::OperationFailed(_))
134        )
135    }
136}