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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use crate::{agent::status::Status, hash_tree::Label, RequestIdError};
use leb128::read;
use std::{
fmt::{Debug, Display, Formatter},
str::Utf8Error,
};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AgentError {
#[error(r#"Invalid Replica URL: "{0}""#)]
InvalidReplicaUrl(String),
#[error("The request timed out.")]
TimeoutWaitingForResponse(),
#[error("The waiter was restarted without being started first.")]
WaiterRestartError(),
#[error("Identity had a signing error: {0}")]
SigningError(String),
#[error("Invalid CBOR data, could not deserialize: {0}")]
InvalidCborData(#[from] serde_cbor::Error),
#[error("Cannot calculate a RequestID: {0}")]
CannotCalculateRequestId(#[from] RequestIdError),
#[error("Candid returned an error: {0}")]
CandidError(Box<dyn Send + Sync + std::error::Error>),
#[error(r#"Cannot parse url: "{0}""#)]
UrlParseError(#[from] url::ParseError),
#[error(r#"Invalid method: "{0}""#)]
InvalidMethodError(#[from] http::method::InvalidMethod),
#[error("Cannot parse Principal: {0}")]
PrincipalError(#[from] crate::export::PrincipalError),
#[error(r#"The Replica returned an error: code {reject_code}, message: "{reject_message}""#)]
ReplicaError {
reject_code: u64,
reject_message: String,
},
#[error("The replica returned an HTTP Error: {0}")]
HttpError(HttpErrorPayload),
#[error("HTTP Authentication cannot be used in a non-secure URL (either HTTPS or localhost)")]
CannotUseAuthenticationOnNonSecureUrl(),
#[error("Password Manager returned an error: {0}")]
AuthenticationError(String),
#[error("Status endpoint returned an invalid status.")]
InvalidReplicaStatus,
#[error("Call was marked as done but we never saw the reply. Request ID: {0}")]
RequestStatusDoneNoReply(String),
#[error("A tool returned a string message error: {0}")]
MessageError(String),
#[error("A tool returned a custom error: {0}")]
CustomError(#[from] Box<dyn Send + Sync + std::error::Error>),
#[error("Error reading LEB128 value: {0}")]
Leb128ReadError(#[from] read::Error),
#[error("Error in UTF-8 string: {0}")]
Utf8ReadError(#[from] Utf8Error),
#[error("The lookup path ({0:?}) is absent in the certificate.")]
LookupPathAbsent(Vec<Label>),
#[error("The lookup path ({0:?}) is unknown in the certificate.")]
LookupPathUnknown(Vec<Label>),
#[error("The lookup path ({0:?}) does not make sense for the certificate.")]
LookupPathError(Vec<Label>),
#[error("The request status ({1}) at path {0:?} is invalid.")]
InvalidRequestStatus(Vec<Label>, String),
#[error("Certificate verification failed.")]
CertificateVerificationFailed(),
#[error("Certificate is not authorized to respond to queries for this canister. While developing: Did you forget to set effective_canister_id?")]
CertificateNotAuthorized(),
#[error(
r#"BLS DER-encoded public key must be ${expected} bytes long, but is {actual} bytes long."#
)]
DerKeyLengthMismatch {
expected: usize,
actual: usize,
},
#[error("BLS DER-encoded public key is invalid. Expected the following prefix: ${expected:?}, but got ${actual:?}")]
DerPrefixMismatch {
expected: Vec<u8>,
actual: Vec<u8>,
},
#[error("The status response did not contain a root key. Status: {0}")]
NoRootKeyInStatus(Status),
#[error("Could not read the root key")]
CouldNotReadRootKey(),
#[error("Failed to initialize the BLS library")]
BlsInitializationFailure(),
#[error("The invocation to the wallet call forward method failed with the error: {0}")]
WalletCallFailed(String),
#[error("The wallet operation failed: {0}")]
WalletError(String),
#[error("The wallet canister must be upgraded: {0}")]
WalletUpgradeRequired(String),
#[error("Missing replica transport in the Agent Builder.")]
MissingReplicaTransport(),
#[error("An error happened during communication with the replica: {0}")]
TransportError(Box<dyn std::error::Error + Send + Sync>),
#[error("There is a mismatch between the CBOR encoded call and the arguments: field {field}, value in argument is {value_arg}, value in CBOR is {value_cbor}")]
CallDataMismatch {
field: String,
value_arg: String,
value_cbor: String,
},
}
impl PartialEq for AgentError {
fn eq(&self, other: &Self) -> bool {
format!("{:?}", self) == format!("{:?}", other)
}
}
pub struct HttpErrorPayload {
pub status: u16,
pub content_type: Option<String>,
pub content: Vec<u8>,
}
impl HttpErrorPayload {
fn fmt_human_readable(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
f.write_fmt(format_args!(
"Http Error: status {}, content type {:?}, content: {}",
http::StatusCode::from_u16(self.status)
.map_or_else(|_| format!("{}", self.status), |code| format!("{}", code)),
self.content_type.clone().unwrap_or_else(|| "".to_string()),
String::from_utf8(self.content.clone()).unwrap_or_else(|_| format!(
"(unable to decode content as UTF-8: {:?})",
self.content
))
))?;
Ok(())
}
}
impl Debug for HttpErrorPayload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
self.fmt_human_readable(f)
}
}
impl Display for HttpErrorPayload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
self.fmt_human_readable(f)
}
}
#[cfg(test)]
mod tests {
use super::HttpErrorPayload;
use crate::AgentError;
#[test]
fn content_type_none_valid_utf8() {
let payload = HttpErrorPayload {
status: 420,
content_type: None,
content: vec![104, 101, 108, 108, 111],
};
assert_eq!(
format!("{}", AgentError::HttpError(payload)),
r#"The replica returned an HTTP Error: Http Error: status 420 <unknown status code>, content type "", content: hello"#,
);
}
#[test]
fn content_type_none_invalid_utf8() {
let payload = HttpErrorPayload {
status: 420,
content_type: None,
content: vec![195, 40],
};
assert_eq!(
format!("{}", AgentError::HttpError(payload)),
r#"The replica returned an HTTP Error: Http Error: status 420 <unknown status code>, content type "", content: (unable to decode content as UTF-8: [195, 40])"#,
);
}
#[test]
fn formats_text_plain() {
let payload = HttpErrorPayload {
status: 420,
content_type: Some("text/plain".to_string()),
content: vec![104, 101, 108, 108, 111],
};
assert_eq!(
format!("{}", AgentError::HttpError(payload)),
r#"The replica returned an HTTP Error: Http Error: status 420 <unknown status code>, content type "text/plain", content: hello"#,
);
}
#[test]
fn formats_text_plain_charset_utf8() {
let payload = HttpErrorPayload {
status: 420,
content_type: Some("text/plain; charset=utf-8".to_string()),
content: vec![104, 101, 108, 108, 111],
};
assert_eq!(
format!("{}", AgentError::HttpError(payload)),
r#"The replica returned an HTTP Error: Http Error: status 420 <unknown status code>, content type "text/plain; charset=utf-8", content: hello"#,
);
}
#[test]
fn formats_text_html() {
let payload = HttpErrorPayload {
status: 420,
content_type: Some("text/html".to_string()),
content: vec![119, 111, 114, 108, 100],
};
assert_eq!(
format!("{}", AgentError::HttpError(payload)),
r#"The replica returned an HTTP Error: Http Error: status 420 <unknown status code>, content type "text/html", content: world"#,
);
}
}