Skip to content

Commit 8a5f3f7

Browse files
author
LorenzoTettamanti
committed
[Code refactoring]: Added documentation for CliError types. Added AgentError type and MonitoringError types. Added From trait implermentation to convert anyhow::Error into CliError. Cargo.lock update
1 parent 756c5f4 commit 8a5f3f7

2 files changed

Lines changed: 70 additions & 29 deletions

File tree

cli/src/essential.rs

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::format;
12
use std::{ collections::BTreeMap, fmt, process::Command, result::Result::Ok };
23

34
use kube::core::ErrorResponse;
@@ -6,17 +7,40 @@ use colored::Colorize;
67

78
use k8s_openapi::api::core::v1::ConfigMap;
89
use k8s_openapi::serde_json::json;
9-
use kube::{Error };
1010
use kube::api::{ Api, ObjectMeta, Patch, PatchParams, PostParams };
1111
use kube::client::Client;
1212

1313
pub static BASE_COMMAND: &str = "kubectl"; // docs: Kubernetes base command
1414

1515
// docs:
1616
//
17-
// Custom enum definition to group all the installation error for cortexflow
17+
// CliError enum to group all the errors
1818
//
19+
// Custom error definition
20+
// InstallerError:
21+
// - used for general installation errors occured during the installation of cortexflow components. Can be used for:
22+
// - Return downloading errors
23+
// - Return unsuccessful file removal during installation
24+
//
25+
// ClientError:
26+
// - used for Kubernetes client errors. Can be used for:
27+
// - Return client connection errors
28+
//
29+
// UninstallError:
30+
// - used for general installation errors occured during the uninstall for cortexflow components. Can be used for:
31+
// - Return components removal errors
32+
//
33+
// AgentError:
34+
// - used for cortexflow agent errors. Can be used for:
35+
// - return errors from the reflection server
36+
// - return unavailable agent errors (404)
37+
//
38+
// MonitoringError:
39+
// - used for general monitoring errors. TODO: currently under implementation
40+
//
41+
// implements fmt::Display for user friendly error messages
1942

43+
#[derive(Debug)]
2044
pub enum CliError {
2145
InstallerError {
2246
reason: String,
@@ -25,38 +49,56 @@ pub enum CliError {
2549
UninstallError {
2650
reason: String,
2751
},
52+
AgentError(tonic_reflection::server::Error),
53+
MonitoringError {
54+
reason: String,
55+
},
2856
}
57+
// docs:
58+
// error type conversions
59+
2960
impl From<kube::Error> for CliError {
30-
fn from(e: Error) -> Self {
61+
fn from(e: kube::Error) -> Self {
3162
CliError::ClientError(e)
3263
}
3364
}
65+
impl From<anyhow::Error> for CliError {
66+
fn from(e: anyhow::Error) -> Self {
67+
CliError::MonitoringError { reason: format!("{}", e) }
68+
}
69+
}
3470

3571
// docs:
36-
//
37-
// Custom error definition
38-
// InstallerError:
39-
// - used for general installation errors occured during the installation of cortexflow components. Can be used for:
40-
// - Return downloading errors
41-
// - Return unsuccessful file removal
42-
//
43-
//
44-
// implements fmt::Display for user-friendly error messages
45-
//
46-
47-
#[derive(Debug, Clone)]
48-
pub struct InstallerError {
49-
pub(crate) reason: String,
50-
}
72+
// fmt::Display implementation for CliError type. Creates a user friendly message error message.
73+
// TODO: implement colored messages using the colorize crate for better output display
5174

52-
impl fmt::Display for InstallerError {
75+
impl fmt::Display for CliError {
5376
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54-
write!(
55-
f,
56-
"An error occured while installing cortexflow components. Reason: {}",
57-
self.reason
58-
);
59-
Ok(())
77+
match self {
78+
CliError::InstallerError { reason } => {
79+
write!(
80+
f,
81+
"An error occured while installing cortexflow components. Reason: {}",
82+
reason
83+
)
84+
}
85+
CliError::UninstallError { reason } => {
86+
write!(
87+
f,
88+
"An error occured while installing cortexflow components. Reason: {}",
89+
reason
90+
)
91+
}
92+
CliError::MonitoringError { reason } => {
93+
write!(
94+
f,
95+
"An error occured while installing cortexflow components. Reason: {}",
96+
reason
97+
)
98+
}
99+
CliError::ClientError(e) => write!(f, "Client Error: {}", e),
100+
CliError::AgentError(e) => write!(f, "Agent Error: {}", e),
101+
}
60102
}
61103
}
62104

@@ -167,7 +209,7 @@ pub async fn read_configs() -> Result<Vec<String>, CliError> {
167209
Err(_) => {
168210
Err(
169211
CliError::ClientError(
170-
Error::Api(ErrorResponse {
212+
kube::Error::Api(ErrorResponse {
171213
status: "failed".to_string(),
172214
message: "Failed to connect to kubernetes client".to_string(),
173215
reason: "Your cluster is probably disconnected".to_string(),
@@ -228,7 +270,7 @@ pub async fn create_config_file(config_struct: MetadataConfigFile) -> Result<(),
228270
Err(_) => {
229271
Err(
230272
CliError::ClientError(
231-
Error::Api(ErrorResponse {
273+
kube::Error::Api(ErrorResponse {
232274
status: "failed".to_string(),
233275
message: "Failed to connect to kubernetes client".to_string(),
234276
reason: "Your cluster is probably disconnected".to_string(),
@@ -336,7 +378,7 @@ pub async fn update_configmap(config_struct: MetadataConfigFile) -> Result<(), C
336378
Err(_) => {
337379
Err(
338380
CliError::ClientError(
339-
Error::Api(ErrorResponse {
381+
kube::Error::Api(ErrorResponse {
340382
status: "failed".to_string(),
341383
message: "Failed to connect to kubernetes client".to_string(),
342384
reason: "Your cluster is probably disconnected".to_string(),

core/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)