Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ license = "Apache-2.0"
readme = "../README.md"

[dependencies]
clap = { version = "4.5.51", features = ["derive"] }
clap = { version = "4.5.54", features = ["derive"] }
colored = "3.0.0"
directories = "6.0.0"
serde = { version = "1.0.219", features = ["derive"] }
tracing = "0.1.41"
tokio = {version = "1.47.0",features = ["macros",'rt-multi-thread']}
tracing = "0.1.44"
tokio = {version = "1.49.0",features = ["macros",'rt-multi-thread']}
anyhow = "1.0.100"
tonic = "0.14.2"
tonic-reflection = "0.14.2"
prost-types = "0.14.1"
prost = "0.14.1"
prost-types = "0.14.3"
prost = "0.14.3"
cortexflow_agent_api = {version = "0.1.1",features = ["client"]}
kube = "2.0.1"
k8s-openapi = {version = "0.26.0", features = ["v1_34"]}
Expand Down
124 changes: 124 additions & 0 deletions cli/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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()
)
}
}
}
}
Loading