1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::fmt::{Debug, Display, Formatter};

/// All errors that can occur in this crate.
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub enum Error<SessionStoreConnectorError> {
    /// A session was attempted to be updated, but the session does not exist.
    /// This may happen due to concurrent modification, and is forbidden to prevent data inconsistencies.
    /// If you receive this error, revert everything that you did while handling the request that
    /// used this session.
    UpdatedSessionDoesNotExist,

    /// Tried as often as desired to generate a session id, but all generated ids already exist.
    MaximumSessionIdGenerationTriesReached,

    /// An error occurred in the session store connector.
    SessionStoreConnector(SessionStoreConnectorError),
}

impl<SessionStoreConnectorError> From<SessionStoreConnectorError>
    for Error<SessionStoreConnectorError>
{
    fn from(error: SessionStoreConnectorError) -> Self {
        Self::SessionStoreConnector(error)
    }
}

impl<SessionStoreConnectorError: Display> Display for Error<SessionStoreConnectorError> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::UpdatedSessionDoesNotExist => write!(f, "the updated session does not exist, which indicates that it was concurrently modified or deleted."),
            Error::MaximumSessionIdGenerationTriesReached => write!(f, "tried to generate a new session id but generated only existing ids until the maximum retry limit was reached."),
            Error::SessionStoreConnector(error) => write!(f, "{error}"),
        }
    }
}

impl<SessionStoreConnectorError: Debug + Display> std::error::Error
    for Error<SessionStoreConnectorError>
{
}