-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patherrors.rs
91 lines (79 loc) · 1.79 KB
/
errors.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
//!
//!
//!
//!
//!
//!
pub type FreightResult = Result<(), FreighterError>;
///
///
#[derive(Debug)]
pub struct FreighterError {
pub error: Option<anyhow::Error>,
pub code: i32,
}
/// The Freighter error is the error type used at Freighter's CLI and others.
///
impl FreighterError {
pub fn new(error: anyhow::Error, code: i32) -> FreighterError {
FreighterError {
error: Some(error),
code,
}
}
pub fn unknown_command(cmd: String) -> FreighterError {
FreighterError {
error: anyhow::anyhow!("Unknown command: {}", cmd).into(),
code: 1,
}
}
pub fn code(code: i32) -> FreighterError {
FreighterError { error: None, code }
}
pub fn print(&self) {
tracing::info!("{}", self.error.as_ref().unwrap());
}
}
///
///
impl From<anyhow::Error> for FreighterError {
fn from(err: anyhow::Error) -> FreighterError {
FreighterError::new(err, 101)
}
}
///
///
impl From<clap::Error> for FreighterError {
fn from(err: clap::Error) -> FreighterError {
let code = i32::from(err.use_stderr());
FreighterError::new(err.into(), code)
}
}
///
///
impl From<std::io::Error> for FreighterError {
fn from(err: std::io::Error) -> FreighterError {
FreighterError::new(err.into(), 1)
}
}
///
///
impl From<git2::Error> for FreighterError {
fn from(err: git2::Error) -> FreighterError {
FreighterError::new(err.into(), 1)
}
}
///
///
impl From<walkdir::Error> for FreighterError {
fn from(err: walkdir::Error) -> FreighterError {
FreighterError::new(err.into(), 1)
}
}
///
///
impl From<reqwest::Error> for FreighterError {
fn from(err: reqwest::Error) -> FreighterError {
FreighterError::new(err.into(), 1)
}
}