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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("hyper error")]
    HyperError(#[from] hyper::Error),
    #[error("std io error")]
    IOError(#[from] std::io::Error),
    #[error("http error")]
    HttpError(#[from] hyper::http::Error),
    #[error("serde_json error")]
    JsonError(#[from] serde_json::Error),
    #[error("decode query string error")]
    QueryError(#[from] serde_urlencoded::de::Error),
    #[error("internal error")]
    Message(String),
    #[error("invalid request header {msg:?}")]
    InvalidHeader { msg: String },
    #[error("missing AppState {msg:?}")]
    MissingAppState { msg: String },
    #[error("missing url param {msg:?}")]
    MissingParam { msg: String },
    #[error("missing cookie {msg:?}")]
    MissingCookie { msg: String },
    #[error("missing header {msg:?}")]
    MissingHeader { msg: String },
    #[error("invalid param {msg:?} as {expected:?}, {err:?}")]
    InvalidParam {
        err: String,
        msg: String,
        expected: &'static str,
    },
}

impl<'a> From<&'a str> for Error {
    fn from(s: &'a str) -> Self {
        Error::Message(s.to_string())
    }
}


impl From<String> for Error {
    fn from(s: String) -> Self {
        Error::Message(s)
    }
}

pub fn invalid_params(
    err: impl std::error::Error,
    msg: impl ToString,
    expected: &'static str,
) -> Error {
    Error::InvalidParam {
        err: err.to_string(),
        msg: msg.to_string(),
        expected,
    }
}

pub fn invalid_header(msg: impl ToString) -> Error {
    Error::InvalidHeader { msg: msg.to_string() }
}

pub fn missing_app_state(msg: impl ToString) -> Error {
    Error::MissingAppState { msg: msg.to_string() }
}

pub fn missing_param(msg: impl ToString) -> Error {
    Error::MissingParam { msg: msg.to_string() }
}

pub fn missing_cookie(msg: impl ToString) -> Error {
    Error::MissingCookie { msg: msg.to_string() }
}

pub fn missing_header(msg: impl ToString) -> Error {
    Error::MissingHeader { msg: msg.to_string() }
}

#[macro_export]
macro_rules! error_message {
    ($message:literal) => {
        $crate::Error::Message($message.to_string())
    };
    ($fmt:expr, $($arg:tt)*) => {
        $crate::Error::Message(format!($fmt, $($arg)*))
    };
}