actix_identity/
error.rs

1//! Failure modes of identity operations.
2
3use actix_session::{SessionGetError, SessionInsertError};
4use actix_web::{cookie::time::error::ComponentRange, http::StatusCode, ResponseError};
5use derive_more::derive::{Display, Error, From};
6
7/// Error that can occur during login attempts.
8#[derive(Debug, Display, Error, From)]
9#[display("{_0}")]
10pub struct LoginError(SessionInsertError);
11
12impl ResponseError for LoginError {
13    fn status_code(&self) -> StatusCode {
14        StatusCode::UNAUTHORIZED
15    }
16}
17
18/// Error encountered when working with a session that has expired.
19#[derive(Debug, Display, Error)]
20#[display("The given session has expired and is no longer valid")]
21pub struct SessionExpiryError(#[error(not(source))] pub(crate) ComponentRange);
22
23/// The identity information has been lost.
24///
25/// Seeing this error in user code indicates a bug in actix-identity.
26#[derive(Debug, Display, Error)]
27#[display(
28    "The identity information in the current session has disappeared after having been \
29           successfully validated. This is likely to be a bug."
30)]
31#[non_exhaustive]
32pub struct LostIdentityError;
33
34/// There is no identity information attached to the current session.
35#[derive(Debug, Display, Error)]
36#[display("There is no identity information attached to the current session")]
37#[non_exhaustive]
38pub struct MissingIdentityError;
39
40/// Encountered a non-string value when loading the user id the session storage.
41#[derive(Debug, Display, Error)]
42#[display("Expected user id to be a String, but found {_0}")]
43pub struct InvalidIdTypeError(#[error(not(source))] pub &'static str);
44
45/// Errors that can occur while retrieving an identity.
46#[derive(Debug, Display, Error, From)]
47#[non_exhaustive]
48pub enum GetIdentityError {
49    /// The session has expired.
50    #[display("{_0}")]
51    SessionExpiryError(SessionExpiryError),
52
53    /// No identity is found in a session.
54    #[display("{_0}")]
55    MissingIdentityError(MissingIdentityError),
56
57    /// Failed to accessing the session store.
58    #[display("{_0}")]
59    SessionGetError(SessionGetError),
60
61    /// Identity info was lost after being validated.
62    ///
63    /// Seeing this error indicates a bug in actix-identity.
64    #[display("{_0}")]
65    LostIdentityError(LostIdentityError),
66
67    /// Encountered a non-string value when loading the user id the session storage.
68    #[display("{_0}")]
69    InvalidIdTypeError(InvalidIdTypeError),
70}
71
72impl ResponseError for GetIdentityError {
73    fn status_code(&self) -> StatusCode {
74        match self {
75            Self::LostIdentityError(_) => StatusCode::INTERNAL_SERVER_ERROR,
76            _ => StatusCode::UNAUTHORIZED,
77        }
78    }
79}