jwx/error.rs
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
use std::fmt::{self, Display, Formatter};
#[derive(Debug, PartialEq)]
pub struct Error {
/// Debug message associated with error
pub msg: &'static str,
pub typ: Type,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", self.typ, self.msg)
}
}
impl std::error::Error for Error {}
/// Type of error encountered.
#[derive(Debug, PartialEq)]
pub enum Type {
/// Token is invalid.
Invalid,
/// Token has expired.
Expired,
/// Not Before (nbf) is set and it's too early to use the token.
Early,
/// Problem with certificate.
Certificate,
/// Problem with key.
Key,
/// Could not download key set.
Connection,
/// Problem with JWT header.
Header,
/// Problem with JWT payload.
Payload,
/// Problem with JWT signature.
Signature,
/// Internal problem (Signals a serious bug or fatal error).
Internal,
}
#[macro_export]
macro_rules! err {
( $typ:ident, $msg:expr ) => {{
Error {
msg: $msg,
typ: Type::$typ,
}
}};
}