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/// Errors that can occur while retrieving an identity.
41#[derive(Debug, Display, Error, From)]
42#[non_exhaustive]
43pub enum GetIdentityError {
44    /// The session has expired.
45    #[display("{_0}")]
46    SessionExpiryError(SessionExpiryError),
47
48    /// No identity is found in a session.
49    #[display("{_0}")]
50    MissingIdentityError(MissingIdentityError),
51
52    /// Failed to accessing the session store.
53    #[display("{_0}")]
54    SessionGetError(SessionGetError),
55
56    /// Identity info was lost after being validated.
57    ///
58    /// Seeing this error indicates a bug in actix-identity.
59    #[display("{_0}")]
60    LostIdentityError(LostIdentityError),
61}
62
63impl ResponseError for GetIdentityError {
64    fn status_code(&self) -> StatusCode {
65        match self {
66            Self::LostIdentityError(_) => StatusCode::INTERNAL_SERVER_ERROR,
67            _ => StatusCode::UNAUTHORIZED,
68        }
69    }
70}