twa_jwks/
error.rs

1use std::fmt;
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug, PartialEq)]
5pub struct Error {
6    /// Debug message associated with error
7    pub msg: &'static str,
8    pub typ: Type,
9}
10
11impl Display for Error {
12    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
13        write!(f, "{:?}: {}", self.typ, self.msg)
14    }
15}
16
17impl std::error::Error for Error {}
18
19/// Type of error encountered
20#[derive(Debug, PartialEq)]
21pub enum Type {
22    /// Token is invalid
23    /// For example, the format of the token is not "HEADER.PAYLOAD.SIGNATURE"
24    Invalid,
25    /// Token has expired
26    Expired,
27    /// Not Before (nbf) is set and it's too early to use the token
28    Early,
29    /// Problem with certificate
30    Certificate,
31    /// Problem with key
32    Key,
33    /// Could not download key set
34    Connection,
35    /// Problem with JWT header
36    Header,
37    /// Problem with JWT payload
38    Payload,
39    /// Problem with JWT signature
40    Signature,
41    /// Internal problem (Signals a serious bug or fatal error)
42    Internal,
43}
44
45pub(crate) fn err(msg: &'static str, typ: Type) -> Error {
46    Error { msg, typ }
47}
48
49pub(crate) fn err_inv(msg: &'static str) -> Error {
50    err(msg, Type::Invalid)
51}
52
53pub(crate) fn err_exp(msg: &'static str) -> Error {
54    err(msg, Type::Expired)
55}
56
57pub(crate) fn err_nbf(msg: &'static str) -> Error {
58    err(msg, Type::Early)
59}
60
61pub(crate) fn err_cer(msg: &'static str) -> Error {
62    err(msg, Type::Certificate)
63}
64
65pub(crate) fn err_key(msg: &'static str) -> Error {
66    err(msg, Type::Key)
67}
68
69pub(crate) fn err_con(msg: &'static str) -> Error {
70    err(msg, Type::Connection)
71}
72
73pub(crate) fn err_hea(msg: &'static str) -> Error {
74    err(msg, Type::Header)
75}
76
77pub(crate) fn err_pay(msg: &'static str) -> Error {
78    err(msg, Type::Payload)
79}
80
81pub(crate) fn err_sig(msg: &'static str) -> Error {
82    err(msg, Type::Signature)
83}
84
85pub(crate) fn err_int(msg: &'static str) -> Error {
86    err(msg, Type::Internal)
87}
88
89#[cfg(test)]
90mod tests {}