1use std::{num, str, string};
2
3#[derive(Debug)]
5pub enum MacaroonError {
6 InitializationError,
9
10 CryptoError(&'static str),
13
14 IncompleteMacaroon(&'static str),
16
17 IncompleteCaveat(&'static str),
19
20 DeserializationError(String),
22
23 CaveatNotSatisfied(String),
27
28 DischargeNotUsed,
32
33 InvalidSignature,
36}
37
38impl From<serde_json::Error> for MacaroonError {
39 fn from(error: serde_json::Error) -> MacaroonError {
40 MacaroonError::DeserializationError(format!("{}", error))
41 }
42}
43
44impl From<string::FromUtf8Error> for MacaroonError {
45 fn from(error: string::FromUtf8Error) -> MacaroonError {
46 MacaroonError::DeserializationError(format!("{}", error))
47 }
48}
49
50impl From<base64::DecodeError> for MacaroonError {
51 fn from(error: base64::DecodeError) -> MacaroonError {
52 MacaroonError::DeserializationError(format!("{}", error))
53 }
54}
55
56impl From<num::ParseIntError> for MacaroonError {
57 fn from(error: num::ParseIntError) -> MacaroonError {
58 MacaroonError::DeserializationError(format!("{}", error))
59 }
60}
61
62impl From<str::Utf8Error> for MacaroonError {
63 fn from(error: str::Utf8Error) -> MacaroonError {
64 MacaroonError::DeserializationError(format!("{}", error))
65 }
66}
67
68impl std::error::Error for MacaroonError {}
69
70impl std::fmt::Display for MacaroonError {
71 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
72 match self {
73 MacaroonError::InitializationError => write!(
74 f,
75 "Failed to initialize cryptographic library for this thread"
76 ),
77 MacaroonError::CryptoError(s) => write!(
78 f,
79 "Error performing lower-level cryptographic function: {}",
80 s
81 ),
82 MacaroonError::IncompleteMacaroon(s) => {
83 write!(f, "Macaroon was missing required field: {}", s)
84 }
85 MacaroonError::IncompleteCaveat(s) => {
86 write!(f, "Caveat was missing required field: {}", s)
87 }
88 MacaroonError::DeserializationError(s) => {
89 write!(f, "Failed to deserialize macaroon: {}", s)
90 }
91 MacaroonError::CaveatNotSatisfied(s) => write!(
92 f,
93 "Macaroon failed to verify because one or more caveats were not satisfied: {}",
94 s
95 ),
96 MacaroonError::DischargeNotUsed => write!(
97 f,
98 "Macaroon failed to verify because one or more discharges were not used"
99 ),
100 MacaroonError::InvalidSignature => write!(
101 f,
102 "Macaroon failed to verify because signature did not match"
103 ),
104 }
105 }
106}