1use snafu::Snafu;
16use tibba_error::Error as BaseError;
17
18pub(crate) const LOG_TARGET: &str = "tibba:session";
21
22mod middleware;
23mod session;
24
25pub use middleware::*;
26pub use session::*;
27
28#[derive(Debug, Snafu)]
29pub enum Error {
30 #[snafu(display("session id is empty"))]
32 SessionIdEmpty,
33 #[snafu(display("session id is invalid"))]
35 SessionIdInvalid,
36 #[snafu(display("session cache is not set"))]
38 SessionCacheNotSet,
39 #[snafu(display("{source}"))]
41 Key { source: cookie::KeyError },
42 #[snafu(display("session not found"))]
44 SessionNotFound,
45 #[snafu(display("user not login"))]
47 UserNotLogin,
48 #[snafu(display("user not admin"))]
50 UserNotAdmin,
51}
52
53impl From<Error> for BaseError {
54 fn from(val: Error) -> Self {
55 let err = match val {
56 e @ (Error::SessionIdEmpty
58 | Error::SessionIdInvalid
59 | Error::SessionCacheNotSet
60 | Error::SessionNotFound) => BaseError::new(e.to_string())
61 .with_status(500)
62 .with_exception(true),
63
64 Error::Key { source } => BaseError::new(source)
66 .with_sub_category("cookie")
67 .with_status(500)
68 .with_exception(true),
69
70 Error::UserNotLogin => BaseError::new("user not login")
72 .with_sub_category("user")
73 .with_status(401)
74 .with_exception(false),
75
76 Error::UserNotAdmin => BaseError::new("user not admin")
78 .with_sub_category("user")
79 .with_status(403)
80 .with_exception(false),
81 };
82 err.with_category("session")
83 }
84}