actix / actix-extras
1
use std::{error::Error as StdError, fmt};
2

3
use actix_web::ResponseError;
4
use derive_more::Display;
5

6
#[derive(Debug, Display)]
7
pub(crate) enum InsertErrorKind {
8
    #[display(fmt = "{}", _0)]
9
    Json(serde_json::Error),
10
}
11

12
impl Into<actix_web::Error> for InsertErrorKind {
13 0
    fn into(self) -> actix_web::Error {
14 0
        match self {
15 0
            InsertErrorKind::Json(err) => err.into(),
16
        }
17
    }
18
}
19

20
/// Error returned by [`Session::insert`][crate::Session::insert]. Allows access to value that
21
/// failed to be inserted.
22
pub struct InsertError<T> {
23
    pub(crate) value: Option<T>,
24
    pub(crate) error: InsertErrorKind,
25
}
26

27
impl<T> InsertError<T> {
28
    /// Takes value out of error.
29
    ///
30
    /// # Panics
31
    /// Panics if called more than once.
32 0
    pub fn take_value(&mut self) -> T {
33 0
        self.value
34
            .take()
35
            .expect("take_value should only be called once")
36
    }
37
}
38

39
impl<T> fmt::Debug for InsertError<T> {
40 0
    fn fmt<'a>(&'a self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 0
        let mut dbg = f.debug_struct("SessionInsertError");
42

43 0
        match &self.value {
44 0
            Some(_) => dbg.field("value", &"Some([value])" as _),
45 0
            None => dbg.field("value", &None::<()> as _),
46
        };
47

48 0
        dbg.field("error", &self.error).finish()
49
    }
50
}
51

52
impl<T> fmt::Display for InsertError<T> {
53 0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 0
        fmt::Display::fmt(&self.error, f)
55
    }
56
}
57

58
impl<T: fmt::Debug> StdError for InsertError<T> {
59 0
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60 0
        Some(match &self.error {
61 0
            InsertErrorKind::Json(err) => err,
62
        })
63
    }
64
}
65

66
impl<T> ResponseError for InsertError<T> {}

Read our documentation on viewing source code .

Loading