r_token/models/
rtoken_error.rs

1//! Error types for the r-token authentication library.
2//!
3//! This module defines error types that can occur during token management operations.
4
5use std::fmt::Formatter;
6
7/// Errors that can occur during token management operations.
8///
9/// This error type implements `std::error::Error` and `actix_web::ResponseError`,
10/// allowing it to be used seamlessly in actix-web handlers.
11///
12/// # Examples
13///
14/// ```rust
15/// use r_token::{RTokenManager, RTokenError};
16///
17/// let manager = RTokenManager::new();
18/// let result = manager.login("user123");
19///
20/// match result {
21///     Ok(token) => println!("Token generated: {}", token),
22///     Err(RTokenError::MutexPoisoned) => eprintln!("Failed to acquire lock"),
23/// }
24/// ```
25#[derive(Debug)]
26pub enum RTokenError {
27    /// The internal mutex protecting the token store has been poisoned.
28    ///
29    /// This typically occurs when a thread panics while holding the lock.
30    /// In most cases, this indicates a critical error in the application.
31    MutexPoisoned,
32}
33
34impl std::fmt::Display for RTokenError {
35    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36        match self {
37            RTokenError::MutexPoisoned => write!(f, "Token manager mutex poisoned"),
38        }
39    }
40}
41
42impl std::error::Error for RTokenError {}
43
44impl actix_web::ResponseError for RTokenError {}