Skip to content

Commit b135228

Browse files
[#171]: cleaned unused dependencies. Fixed error propagation from Result type with ? operator for the apply_component() function in install.rs module. Fixed stack overflow error while returning a custom error. implemented coloured error outputs,added docs in error.rs module
1 parent 8f77322 commit b135228

5 files changed

Lines changed: 47 additions & 63 deletions

File tree

cli/src/errors.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ use std::fmt;
1515
// - used for Kubernetes client errors. Can be used for:
1616
// - Return client connection errors
1717
//
18-
// UninstallError:
19-
// - used for general installation errors occured during the uninstall for cortexflow components. Can be used for:
20-
// - Return components removal errors
21-
//
2218
// AgentError:
2319
// - used for cortexflow agent errors. Can be used for:
2420
// - return errors from the reflection server
@@ -33,12 +29,15 @@ use std::fmt;
3329
pub enum CliError {
3430
InstallerError { reason: String },
3531
ClientError(kube::Error),
36-
UninstallError { reason: String },
3732
AgentError(tonic_reflection::server::Error),
3833
MonitoringError { reason: String },
3934
}
4035
// docs:
41-
// error type conversions
36+
//
37+
// The following functions implements the trait From conversions
38+
//
39+
// The From Trait is used to perform a value-to-value conversion while consuming input values.
40+
// We use that to return a single error type 'CliError' that incapsulates multiple error types
4241

4342
impl From<kube::Error> for CliError {
4443
fn from(e: kube::Error) -> Self {
@@ -48,29 +47,28 @@ impl From<kube::Error> for CliError {
4847
impl From<anyhow::Error> for CliError {
4948
fn from(e: anyhow::Error) -> Self {
5049
CliError::MonitoringError {
51-
reason: format!("{}", e),
50+
reason: e.to_string(),
5251
}
5352
}
5453
}
55-
impl From<()> for CliError {
56-
fn from(e: ()) -> Self {
57-
return ().into();
58-
}
59-
}
6054
impl From<prost::DecodeError> for CliError {
6155
fn from(e: prost::DecodeError) -> Self {
62-
todo!()
56+
return CliError::AgentError(tonic_reflection::server::Error::DecodeError(e));
6357
}
6458
}
6559
impl From<tonic::Status> for CliError {
6660
fn from(e: tonic::Status) -> Self {
67-
todo!()
61+
return CliError::MonitoringError {
62+
reason: e.to_string(),
63+
};
6864
}
6965
}
7066

7167
// docs:
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
68+
//
69+
// The Trait fmt::Display is used to create a user friendly error message for the CliError type.
70+
// This Trait automatically implements the ToString trait for the type allowing
71+
// the usage of .to_string() method
7472

7573
impl fmt::Display for CliError {
7674
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -83,31 +81,36 @@ impl fmt::Display for CliError {
8381
"An error occured while installing cortexflow components. Reason:"
8482
.bold()
8583
.red(),
86-
reason
84+
reason.red().bold()
8785
)
8886
}
89-
CliError::UninstallError { reason } => {
87+
CliError::MonitoringError { reason } => {
9088
write!(
9189
f,
92-
"An error occured while installing cortexflow components. Reason: {}",
93-
reason
90+
"{} {} {}",
91+
"=====>".blue().bold(),
92+
"An error occured while installing cortexflow components. Reason:"
93+
.bold()
94+
.red(),
95+
reason.red().bold()
9496
)
9597
}
96-
CliError::MonitoringError { reason } => {
98+
CliError::ClientError(e) => {
9799
write!(
98100
f,
99-
"An error occured while installing cortexflow components. Reason: {}",
100-
reason
101+
"{} {} {}",
102+
"=====>".blue().bold(),
103+
"Client Error:".bold().red(),
104+
e.to_string().red().bold()
101105
)
102106
}
103-
CliError::ClientError(e) => write!(f, "Client Error: {}", e),
104107
CliError::AgentError(e) => {
105108
write!(
106109
f,
107110
"{} {} {}",
108111
"=====>".bold().blue(),
109112
"Agent Error:".bold().red(),
110-
e
113+
e.to_string().bold().red()
111114
)
112115
}
113116
}

cli/src/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn install_components(components_type: &str) -> Result<(), CliError> {
247247
"Applying",
248248
component.to_string().green().bold()
249249
);
250-
apply_component(component);
250+
apply_component(component)?;
251251
i = i + 1;
252252
}
253253
} else if components_type == "simple-example" {
@@ -267,7 +267,7 @@ fn install_components(components_type: &str) -> Result<(), CliError> {
267267
"Applying",
268268
component.to_string().green().bold()
269269
);
270-
apply_component(component);
270+
apply_component(component)?;
271271
i = i + 1;
272272
}
273273
} else {

cli/src/main.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,14 @@ async fn args_parser() -> Result<(), CliError> {
7676
match args.cmd {
7777
Some(Commands::Install(installation_args)) => match installation_args.install_cmd {
7878
InstallCommands::All => {
79-
install_cortexflow().await.map_err(|e| eprintln!("{}", e))?;
79+
install_cortexflow().await?;
8080
}
8181
InstallCommands::TestPods => {
82-
install_simple_example()
83-
.await
84-
.map_err(|e| eprintln!("{}", e))?;
82+
install_simple_example().await?;
8583
}
8684
},
8785
Some(Commands::Uninstall) => {
88-
uninstall().await.map_err(|e| eprintln!("{}", e))?;
86+
uninstall().await?;
8987
}
9088
Some(Commands::Update) => {
9189
update_cli();
@@ -95,53 +93,39 @@ async fn args_parser() -> Result<(), CliError> {
9593
}
9694
Some(Commands::Service(service_args)) => match service_args.service_cmd {
9795
ServiceCommands::List { namespace } => {
98-
list_services(namespace)
99-
.await
100-
.map_err(|e| eprintln!("{}", e))?;
96+
list_services(namespace).await?;
10197
}
10298
ServiceCommands::Describe {
10399
service_name,
104100
namespace,
105101
} => {
106-
describe_service(service_name, &namespace)
107-
.await
108-
.map_err(|e| eprintln!("{}", e))?;
102+
describe_service(service_name, &namespace).await?;
109103
}
110104
},
111105
Some(Commands::Status(status_args)) => {
112-
status_command(status_args.output, status_args.namespace)
113-
.await
114-
.map_err(|e| eprintln!("{}", e))?;
106+
status_command(status_args.output, status_args.namespace).await?;
115107
}
116108
Some(Commands::Logs(logs_args)) => {
117-
logs_command(logs_args.service, logs_args.component, logs_args.namespace)
118-
.await
119-
.map_err(|e| eprintln!("{}", e))?;
109+
logs_command(logs_args.service, logs_args.component, logs_args.namespace).await?;
120110
}
121111
Some(Commands::Monitor(monitor_args)) => match monitor_args.monitor_cmd {
122112
MonitorCommands::List => {
123-
let _ = list_features().await.map_err(|e| eprintln!("{}", e))?;
113+
let _ = list_features().await?;
124114
}
125115
MonitorCommands::Connections => {
126-
let _ = monitor_identity_events()
127-
.await
128-
.map_err(|e| eprintln!("{}", e))?;
116+
let _ = monitor_identity_events().await?;
129117
}
130118
MonitorCommands::Latencymetrics => {
131-
let _ = monitor_latency_metrics()
132-
.await
133-
.map_err(|e| eprintln!("{}", e))?;
119+
let _ = monitor_latency_metrics().await?;
134120
}
135121
MonitorCommands::Droppedpackets => {
136-
let _ = monitor_dropped_packets()
137-
.await
138-
.map_err(|e| eprintln!("{}", e))?;
122+
let _ = monitor_dropped_packets().await?;
139123
}
140124
},
141125
Some(Commands::Policies(policies_args)) => {
142126
match policies_args.policy_cmd {
143127
PoliciesCommands::CheckBlocklist => {
144-
let _ = check_blocklist().await.map_err(|e| eprintln!("{}", e))?;
128+
let _ = check_blocklist().await?;
145129
}
146130
PoliciesCommands::CreateBlocklist => {
147131
// pass the ip as a monitoring flag
@@ -155,9 +139,7 @@ async fn args_parser() -> Result<(), CliError> {
155139
match create_blocklist(&ip).await {
156140
Ok(_) => {
157141
//update the config metadata
158-
let _ = update_config_metadata(&ip, "add")
159-
.await
160-
.map_err(|e| eprintln!("{}", e))?;
142+
let _ = update_config_metadata(&ip, "add").await?;
161143
}
162144
Err(e) => {
163145
eprintln!("{}", e);
@@ -177,9 +159,7 @@ async fn args_parser() -> Result<(), CliError> {
177159
println!("Inserted ip: {}", ip);
178160
match remove_ip(&ip).await {
179161
Ok(_) => {
180-
let _ = update_config_metadata(&ip, "delete")
181-
.await
182-
.map_err(|e| eprintln!("{}", e))?;
162+
let _ = update_config_metadata(&ip, "delete").await?;
183163
}
184164
Err(e) => {
185165
eprintln!("{}", e);
@@ -198,5 +178,5 @@ async fn args_parser() -> Result<(), CliError> {
198178

199179
#[tokio::main]
200180
async fn main() {
201-
let _ = args_parser().await;
181+
let _ = args_parser().await.map_err(|e| eprintln!("{}", e));
202182
}

cli/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::{Args, Subcommand};
22
use colored::Colorize;
3+
use kube::Client;
34
use kube::{Error, core::ErrorResponse};
45
use std::{process::Command, str};
56

cli/src/uninstall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use colored::Colorize;
2-
use std::{io::stdin, process::Command, thread, time::Duration};
2+
use std::{io::stdin, process::Command};
33

44
use crate::errors::CliError;
55
use crate::essential::{BASE_COMMAND, connect_to_client};

0 commit comments

Comments
 (0)