Repository navigation
Expand file tree
/
Copy patherrors.rs
More file actions
139 lines (132 loc) · 4.23 KB
/
Copy patherrors.rs
File metadata and controls
139 lines (132 loc) · 4.23 KB
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
use colored::Colorize;
use std::{error::Error, fmt};
// docs:
//
// CliError enum to group all the errors
//
// Custom error definition
//
// BaseError:
// - used for general errors
//
// InstallerError:
// - used for general installation errors occured during the installation of cortexflow components. Can be used for:
// - Return downloading errors
// - Return unsuccessful file removal during installation
//
// ClientError:
// - used for Kubernetes client errors. Can be used for:
// - Return client connection errors
//
// AgentError:
// - used for cortexflow agent errors. Can be used for:
// - return errors from the reflection server
// - return unavailable agent errors (404)
//
//
// implements fmt::Display for user friendly error messages
#[derive(Debug)]
pub enum CliError {
InstallerError { reason: String },
ClientError(kube::Error),
AgentError(tonic_reflection::server::Error),
BaseError { reason: String },
}
// docs:
//
// The following functions implements the trait From conversions
//
// The From Trait is used to perform a value-to-value conversion while consuming input values.
// We use that to return a single error type 'CliError' that incapsulates multiple error types
impl From<kube::Error> for CliError {
fn from(e: kube::Error) -> Self {
CliError::ClientError(e)
}
}
impl From<anyhow::Error> for CliError {
fn from(e: anyhow::Error) -> Self {
CliError::BaseError {
reason: e.to_string(),
}
}
}
impl From<prost::DecodeError> for CliError {
fn from(e: prost::DecodeError) -> Self {
return CliError::AgentError(tonic_reflection::server::Error::DecodeError(e));
}
}
impl From<tonic::Status> for CliError {
fn from(e: tonic::Status) -> Self {
return CliError::BaseError {
reason: e.to_string(),
};
}
}
// docs:
//
// The Trait fmt::Display is used to create a user friendly error message for the CliError type.
// This Trait automatically implements the ToString trait for the type allowing
// the usage of .to_string() method
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CliError::InstallerError { reason } => {
write!(
f,
"{} {} {}",
"=====>".blue().bold(),
"An error occured while installing cortexflow components. Reason:"
.bold()
.red(),
reason.red().bold()
)
}
CliError::BaseError { reason } => {
write!(
f,
"{} {} {}",
"=====>".blue().bold(),
"An error occured. Reason:"
.bold()
.red(),
reason.red().bold()
)
}
CliError::ClientError(e) => {
// raw error looks like this
// (ErrorResponse { status: "failed", message: "Failed to connect to kubernetes client", reason: "transport error", code: 404 }
let msg = Error::source(e).unwrap(); // msg = Failed to connect to kubernetes client: transport error
write!(
f,
"{} {} {}",
"=====>".blue().bold(),
"Client Error:".bold().red(),
msg.to_string().red().bold()
)
}
CliError::AgentError(e) => {
let msg = Error::source(e).unwrap();
write!(
f,
"{} {} {}",
"=====>".bold().blue(),
"Agent Error:".bold().red(),
msg.to_string().bold().red()
)
}
}
}
}
# [cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display_base_error() {
let err = CliError::BaseError {
reason: "some reason".to_string(),
};
let err_str = format!("{}", err);
assert!(err_str.contains("An error occured. Reason:"));
assert!(err_str.contains("some reason"));
}
}