Skip to main content

trojan_auth/
error.rs

1//! Authentication error types.
2
3/// Authentication error.
4#[derive(Debug, thiserror::Error)]
5pub enum AuthError {
6    /// Invalid credentials provided.
7    #[error("invalid credential")]
8    Invalid,
9
10    /// Backend error (database, network, etc.).
11    #[error("backend error: {0}")]
12    Backend(String),
13
14    /// Rate limited.
15    #[error("rate limited")]
16    RateLimited,
17
18    /// User not found.
19    #[error("user not found")]
20    NotFound,
21
22    /// Traffic limit exceeded.
23    #[error("traffic limit exceeded")]
24    TrafficExceeded,
25
26    /// Account expired.
27    #[error("account expired")]
28    Expired,
29
30    /// Account disabled.
31    #[error("account disabled")]
32    Disabled,
33}
34
35impl AuthError {
36    /// Create a backend error from any error type.
37    #[inline]
38    pub fn backend<E: std::fmt::Display>(err: E) -> Self {
39        Self::Backend(err.to_string())
40    }
41}
42
43// SQLx error conversion (only when sql feature enabled)
44#[cfg(feature = "sql")]
45impl From<sqlx::Error> for AuthError {
46    fn from(err: sqlx::Error) -> Self {
47        match err {
48            sqlx::Error::RowNotFound => AuthError::NotFound,
49            _ => AuthError::backend(err),
50        }
51    }
52}