forked from awslabs/tough
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.rs
396 lines (333 loc) · 10.8 KB
/
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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT OR Apache-2.0
// Not really worried about the memory penalty of large enum variants here
#![allow(clippy::large_enum_variant)]
#![allow(clippy::default_trait_access)]
use snafu::{Backtrace, Snafu};
use std::path::PathBuf;
pub(crate) type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub(crate) enum Error {
#[snafu(display("Failed to clone repository: {}", source))]
CloneRepository {
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to run {}: {}", command_str, source))]
CommandExec {
command_str: String,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Command {} failed with {}", command_str, status))]
CommandStatus {
command_str: String,
status: std::process::ExitStatus,
backtrace: Backtrace,
},
#[snafu(display("Command {} output is not valid UTF-8: {}", command_str, source))]
CommandUtf8 {
command_str: String,
source: std::string::FromUtf8Error,
backtrace: Backtrace,
},
#[snafu(display("Cannot determine current directory: {}", source))]
CurrentDir {
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Date argument '{}' is invalid: {}", input, msg))]
DateArgInvalid { input: String, msg: String },
#[snafu(display(
"Date argument had count '{}' that failed to parse as integer: {}",
input,
source
))]
DateArgCount {
input: String,
source: std::num::ParseIntError,
},
#[snafu(display("Failed to create directory '{}': {}", path.display(), source))]
DirCreate {
path: PathBuf,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Invalid delegation structure: {}", source))]
DelegationStructure {
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Couldn't find role '{}': {}", role, source))]
DelegateeNotFound {
role: String,
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("A file or directory already exists at '{}'", path.display()))]
DownloadOutdirExists { path: PathBuf, backtrace: Backtrace },
#[snafu(display(
"Failed to create a Repository Editor with root.json '{}': {}",
path.display(),
source
))]
EditorCreate {
path: PathBuf,
source: tough::error::Error,
},
#[snafu(display("Failed to create a RepositoryEditor from an existing repo with root.json '{}': {}", path.display(), source))]
EditorFromRepo {
path: PathBuf,
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to open {}: {}", path.display(), source))]
FileOpen {
path: PathBuf,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to parse {}: {}", path.display(), source))]
FileParseJson {
path: PathBuf,
source: serde_json::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to copy {} to {}: {}", source.file.path().display(), path.display(), source.error))]
FilePersist {
path: PathBuf,
source: tempfile::PersistError,
backtrace: Backtrace,
},
#[snafu(display("Failed to create temporary file in {}: {}", path.display(), source))]
FileTempCreate {
path: PathBuf,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Can't build URL from path '{}'", path.display()))]
FileUrl { path: PathBuf, backtrace: Backtrace },
#[snafu(display("Failed to write to {}: {}", path.display(), source))]
FileWrite {
path: PathBuf,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to write to {}: {}", path.display(), source))]
FileWriteJson {
path: PathBuf,
source: serde_json::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to initialize global thread pool: {}", source))]
InitializeThreadPool {
source: rayon::ThreadPoolBuildError,
backtrace: Backtrace,
},
#[snafu(display("Invalid target name: {}", source))]
InvalidTargetName { source: tough::error::Error },
#[snafu(display("Failed to serialize to JSON: {}", source))]
JsonSerialization {
source: tough::schema::Error,
backtrace: Backtrace,
},
#[snafu(display("Duplicate key ID: {}", key_id))]
KeyDuplicate {
key_id: String,
backtrace: Backtrace,
},
#[snafu(display("Failed to calculate key ID: {}", source))]
KeyId {
#[snafu(backtrace)]
source: tough::schema::Error,
},
#[snafu(display("Unable to parse keypair: {}", source))]
KeyPairParse {
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Unable to parse keypair: {}", source))]
KeyPairFromKeySource {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
backtrace: Backtrace,
},
#[snafu(display(
"Failed to symlink target data from '{}' to '{}': {}",
indir.display(),
outdir.display(),
source
))]
LinkTargets {
indir: PathBuf,
outdir: PathBuf,
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Unable to initialize logger: {}", source))]
Logger {
source: log::SetLoggerError,
backtrace: Backtrace,
},
#[snafu(display("Unable to load incoming metadata: {}", source))]
LoadMetadata {
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Metadata error: {}", source))]
Metadata {
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Missing: {}", what))]
Missing { what: String, backtrace: Backtrace },
#[snafu(display("Unable to determine file name from path: '{}'", path.display()))]
NoFileName { path: PathBuf, backtrace: Backtrace },
#[snafu(display("Failed to open file {}: {}", path.display(), source))]
OpenFile {
path: PathBuf,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to open trusted root metadata file {}: {}", path.display(), source))]
OpenRoot {
path: PathBuf,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Path {} does not have a parent", path.display()))]
PathParent { path: PathBuf, backtrace: Backtrace },
#[snafu(display("Path {} is not valid UTF-8", path.display()))]
PathUtf8 { path: PathBuf, backtrace: Backtrace },
#[snafu(display("Failed to load repository: {}", source))]
RepoLoad {
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to copy from response: {}", source))]
ReqwestCopy {
source: reqwest::Error,
backtrace: Backtrace,
},
#[snafu(display("Error making request: {}", source))]
ReqwestGet {
source: reqwest::Error,
backtrace: Backtrace,
},
#[snafu(display("Response '{}' from '{}': {}", get_status_code(source), url, source))]
BadResponse {
url: String,
source: reqwest::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to sign repository: {}", source))]
SignRepo {
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display(
"Root was signed with {} signatures; it must be signed with at least {}",
signature_count,
threshold,
))]
SignatureRoot {
threshold: u64,
signature_count: usize,
},
#[snafu(display("Failed to sign '{}': {}", path.display(), source))]
SignRoot {
path: PathBuf,
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Unable to create Target from path '{}': {}", path.display(), source))]
TargetFromPath {
path: PathBuf,
source: tough::schema::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to add targets from directory '{}': {}", dir.display(), source))]
TargetsFromDir {
dir: PathBuf,
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Target not found: {}", target))]
TargetNotFound {
target: String,
backtrace: Backtrace,
},
#[snafu(display("Failed to create temporary directory: {}", source))]
TempDir {
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Unrecognized URL scheme \"{}\"", scheme))]
UnrecognizedScheme {
scheme: String,
backtrace: Backtrace,
},
/// Root creates an unloadable repo
#[snafu(display(
"Unstable root: '{}' role contains {} keys, threshold is {}",
role,
actual,
threshold
))]
UnstableRoot {
role: tough::schema::RoleType,
threshold: u64,
actual: usize,
},
#[snafu(display("Failed to parse URL \"{}\": {}", url, source))]
UrlParse {
url: String,
source: url::ParseError,
backtrace: Backtrace,
},
#[snafu(display("Version number overflow"))]
VersionOverflow { backtrace: Backtrace },
#[snafu(display("Version number is zero"))]
VersionZero { backtrace: Backtrace },
#[snafu(display("Failed to walk directory tree '{}': {}", directory.display(), source))]
WalkDir {
directory: PathBuf,
source: walkdir::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed write: {}", source))]
WriteKeySource {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
backtrace: Backtrace,
},
#[snafu(display("Failed writing repo data to disk at '{}': {}", directory.display(), source))]
WriteRepo {
directory: PathBuf,
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Unable to write roles: {:?}", roles))]
WriteRoles {
roles: Vec<String>,
source: tough::error::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed writing target data to disk: {}", source))]
WriteTarget {
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("Failed to join a task: {}", source))]
JoinTask {
source: tokio::task::JoinError,
backtrace: Backtrace,
},
}
// Extracts the status code from a reqwest::Error and converts it to a string to be displayed
fn get_status_code(source: &reqwest::Error) -> String {
source
.status()
.as_ref()
.map_or("Unknown", reqwest::StatusCode::as_str)
.to_string()
}