jwx/
error.rs

1use std::fmt::{self, Display, Formatter};
2
3#[derive(Debug, PartialEq)]
4pub struct Error {
5    /// Debug message associated with error
6    pub msg: &'static str,
7    pub typ: Type,
8}
9
10impl Display for Error {
11    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
12        write!(f, "{:?}: {}", self.typ, self.msg)
13    }
14}
15
16impl std::error::Error for Error {}
17
18/// Type of error encountered.
19#[derive(Debug, PartialEq)]
20pub enum Type {
21    /// Token is invalid.
22    Invalid,
23    /// Token has expired.
24    Expired,
25    /// Not Before (nbf) is set and it's too early to use the token.
26    Early,
27    /// Problem with certificate.
28    Certificate,
29    /// Problem with key.
30    Key,
31    /// Could not download key set.
32    Connection,
33    /// Problem with JWT header.
34    Header,
35    /// Problem with JWT payload.
36    Payload,
37    /// Problem with JWT signature.
38    Signature,
39    /// Internal problem (Signals a serious bug or fatal error).
40    Internal,
41}
42
43#[macro_export]
44macro_rules! err {
45    ( $typ:ident, $msg:expr ) => {{
46        Error {
47            msg: $msg,
48            typ: Type::$typ,
49        }
50    }};
51}