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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use {
http::status::StatusCode,
scratchstack_errors::ServiceError,
std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
io::Error as IOError,
},
};
const ERR_CODE_EXPIRED_TOKEN: &str = "ExpiredToken";
const ERR_CODE_INTERNAL_FAILURE: &str = "InternalFailure";
const ERR_CODE_INVALID_CONTENT_TYPE: &str = "InvalidContentType";
const ERR_CODE_INVALID_BODY_ENCODING: &str = "InvalidBodyEncoding";
const ERR_CODE_INVALID_CLIENT_TOKEN_ID: &str = "InvalidClientTokenId";
const ERR_CODE_INCOMPLETE_SIGNATURE: &str = "IncompleteSignature";
const ERR_CODE_INVALID_REQUEST_METHOD: &str = "InvalidRequestMethod";
const ERR_CODE_INVALID_URI_PATH: &str = "InvalidURIPath";
const ERR_CODE_MALFORMED_QUERY_STRING: &str = "MalformedQueryString";
const ERR_CODE_MISSING_AUTHENTICATION_TOKEN: &str = "MissingAuthenticationToken";
const ERR_CODE_SIGNATURE_DOES_NOT_MATCH: &str = "SignatureDoesNotMatch";
#[derive(Debug)]
pub enum SignatureError {
ExpiredToken(String),
IO(IOError),
InternalServiceError(Box<dyn Error + Send + Sync>),
InvalidBodyEncoding(String),
InvalidClientTokenId(String),
InvalidContentType(String),
InvalidRequestMethod(String),
IncompleteSignature(String),
InvalidURIPath(String),
MalformedQueryString(String),
MissingAuthenticationToken(String),
SignatureDoesNotMatch(Option<String>),
}
impl SignatureError {
fn error_code(&self) -> &'static str {
match self {
Self::ExpiredToken(_) => ERR_CODE_EXPIRED_TOKEN,
Self::IO(_) | Self::InternalServiceError(_) => ERR_CODE_INTERNAL_FAILURE,
Self::InvalidBodyEncoding(_) => ERR_CODE_INVALID_BODY_ENCODING,
Self::InvalidClientTokenId(_) => ERR_CODE_INVALID_CLIENT_TOKEN_ID,
Self::InvalidContentType(_) => ERR_CODE_INVALID_CONTENT_TYPE,
Self::InvalidRequestMethod(_) => ERR_CODE_INVALID_REQUEST_METHOD,
Self::IncompleteSignature(_) => ERR_CODE_INCOMPLETE_SIGNATURE,
Self::InvalidURIPath(_) => ERR_CODE_INVALID_URI_PATH,
Self::MalformedQueryString(_) => ERR_CODE_MALFORMED_QUERY_STRING,
Self::MissingAuthenticationToken(_) => ERR_CODE_MISSING_AUTHENTICATION_TOKEN,
Self::SignatureDoesNotMatch(_) => ERR_CODE_SIGNATURE_DOES_NOT_MATCH,
}
}
fn http_status(&self) -> StatusCode {
match self {
Self::IncompleteSignature(_)
| Self::InvalidBodyEncoding(_)
| Self::InvalidRequestMethod(_)
| Self::InvalidURIPath(_)
| Self::MalformedQueryString(_)
| Self::MissingAuthenticationToken(_) => StatusCode::BAD_REQUEST,
Self::IO(_) | Self::InternalServiceError(_) => StatusCode::INTERNAL_SERVER_ERROR,
_ => StatusCode::FORBIDDEN,
}
}
}
impl ServiceError for SignatureError {
fn error_code(&self) -> &'static str {
SignatureError::error_code(self)
}
fn http_status(&self) -> StatusCode {
SignatureError::http_status(self)
}
}
impl Display for SignatureError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self {
Self::ExpiredToken(msg) => f.write_str(msg),
Self::IO(ref e) => Display::fmt(e, f),
Self::InternalServiceError(ref e) => Display::fmt(e, f),
Self::InvalidBodyEncoding(msg) => f.write_str(msg),
Self::InvalidClientTokenId(msg) => f.write_str(msg),
Self::InvalidContentType(msg) => f.write_str(msg),
Self::InvalidRequestMethod(msg) => f.write_str(msg),
Self::IncompleteSignature(msg) => f.write_str(msg),
Self::InvalidURIPath(msg) => f.write_str(msg),
Self::MalformedQueryString(msg) => f.write_str(msg),
Self::MissingAuthenticationToken(msg) => f.write_str(msg),
Self::SignatureDoesNotMatch(msg) => {
if let Some(msg) = msg {
f.write_str(msg)
} else {
Ok(())
}
}
}
}
}
impl Error for SignatureError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::IO(ref e) => Some(e),
_ => None,
}
}
}
impl From<IOError> for SignatureError {
fn from(e: IOError) -> SignatureError {
SignatureError::IO(e)
}
}
impl From<Box<dyn Error + Send + Sync>> for SignatureError {
fn from(e: Box<dyn Error + Send + Sync>) -> SignatureError {
match e.downcast::<SignatureError>() {
Ok(sig_err) => *sig_err,
Err(e) => SignatureError::InternalServiceError(e),
}
}
}
#[cfg(test)]
mod tests {
use {crate::SignatureError, std::error::Error};
#[test_log::test]
fn test_from() {
let utf8_error = Box::new(String::from_utf8(b"\x80".to_vec()).unwrap_err());
let e: SignatureError = (utf8_error as Box<dyn Error + Send + Sync + 'static>).into();
assert_eq!(e.error_code(), "InternalFailure");
assert_eq!(e.http_status(), 500);
let e = SignatureError::MalformedQueryString("foo".to_string());
let e2 = SignatureError::from(Box::new(e) as Box<dyn Error + Send + Sync + 'static>);
assert_eq!(e2.to_string(), "foo");
assert_eq!(e2.error_code(), "MalformedQueryString");
}
}