Skip to content

Commit

Permalink
Merge branch 'main' into refactor/tailcallhq#1400/move-server-respons…
Browse files Browse the repository at this point in the history
…eHeaders-to-server-headers-custom
  • Loading branch information
tusharmath authored Mar 13, 2024
2 parents 5e0ae9c + 4eb2bc5 commit 542ce18
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
63 changes: 62 additions & 1 deletion src/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use thiserror::Error;

use crate::valid::ValidationError;

#[derive(Debug, Error, Setters)]
#[derive(Debug, Error, Setters, PartialEq, Clone)]
pub struct CLIError {
is_root: bool,
#[setters(skip)]
Expand Down Expand Up @@ -156,6 +156,31 @@ impl From<rustls::Error> for CLIError {
}
}

impl From<anyhow::Error> for CLIError {
fn from(error: anyhow::Error) -> Self {
// Convert other errors to CLIError
let cli_error = match error.downcast::<CLIError>() {
Ok(cli_error) => cli_error,
Err(error) => {
// Convert other errors to CLIError
let cli_error = match error.downcast::<ValidationError<String>>() {
Ok(validation_error) => CLIError::from(validation_error),
Err(error) => {
let sources = error
.source()
.map(|error| vec![CLIError::new(error.to_string().as_str())])
.unwrap_or_default();

CLIError::new(&error.to_string()).caused_by(sources)
}
};
cli_error
}
};
cli_error
}
}

impl From<std::io::Error> for CLIError {
fn from(error: std::io::Error) -> Self {
let cli_error = CLIError::new("IO Error");
Expand Down Expand Up @@ -353,4 +378,40 @@ mod tests {

assert_eq!(error.to_string(), expected);
}

#[test]
fn test_cli_error_identity() {
let cli_error = CLIError::new("Server could not be started")
.description("The port is already in use".to_string())
.trace(vec!["@server".into(), "port".into()]);
let anyhow_error: anyhow::Error = cli_error.clone().into();

let actual = CLIError::from(anyhow_error);
let expected = cli_error;

assert_eq!(actual, expected);
}

#[test]
fn test_validation_error_identity() {
let validation_error = ValidationError::from(
Cause::new("Test Error".to_string()).trace(vec!["Query".to_string()]),
);
let anyhow_error: anyhow::Error = validation_error.clone().into();

let actual = CLIError::from(anyhow_error);
let expected = CLIError::from(validation_error);

assert_eq!(actual, expected);
}

#[test]
fn test_generic_error() {
let anyhow_error = anyhow::anyhow!("Some error msg");

let actual: CLIError = CLIError::from(anyhow_error);
let expected = CLIError::new("Some error msg");

assert_eq!(actual, expected);
}
}
20 changes: 1 addition & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::cell::Cell;
use mimalloc::MiMalloc;
use tailcall::cli::CLIError;
use tailcall::tracing::default_tailcall_tracing;
use tailcall::valid::ValidationError;
use tracing::subscriber::DefaultGuard;

#[global_allocator]
Expand Down Expand Up @@ -47,24 +46,7 @@ fn main() -> anyhow::Result<()> {
Ok(_) => {}
Err(error) => {
// Ensure all errors are converted to CLIErrors before being printed.
let cli_error = match error.downcast::<CLIError>() {
Ok(cli_error) => cli_error,
Err(error) => {
// Convert other errors to CLIError
let cli_error = match error.downcast::<ValidationError<String>>() {
Ok(validation_error) => CLIError::from(validation_error),
Err(error) => {
let sources = error
.source()
.map(|error| vec![CLIError::new(error.to_string().as_str())])
.unwrap_or_default();

CLIError::new(&error.to_string()).caused_by(sources)
}
};
cli_error
}
};
let cli_error: CLIError = error.into();
tracing::error!("{}", cli_error.color(true));
std::process::exit(exitcode::CONFIG);
}
Expand Down

0 comments on commit 542ce18

Please sign in to comment.