Skip to content

Commit

Permalink
Merge pull request #1675 from fermyon/remove-spurious-failure
Browse files Browse the repository at this point in the history
Exit early when trigger process fails
  • Loading branch information
rylev authored Jul 25, 2023
2 parents 5d56d2b + 966113f commit 4be22a2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
1 change: 0 additions & 1 deletion crates/terminal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ fn color_choice(stream: atty::Stream) -> termcolor::ColorChoice {
#[macro_export]
macro_rules! step {
($step:expr, $($arg:tt)*) => {{

$crate::cprint!($crate::colors::bold_green(), $step);
print!(" ");
println!($($arg)*);
Expand Down
19 changes: 15 additions & 4 deletions src/bin/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::Error;
use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
use is_terminal::IsTerminal;
use lazy_static::lazy_static;
use spin_cli::build_info::*;
use spin_cli::commands::external::predefined_externals;
use spin_cli::commands::{
build::BuildCommand,
Expand All @@ -16,6 +15,7 @@ use spin_cli::commands::{
up::UpCommand,
watch::WatchCommand,
};
use spin_cli::{build_info::*, subprocess::ExitStatusError};
use spin_redis_engine::RedisTrigger;
use spin_trigger::cli::help::HelpArgsOnlyTrigger;
use spin_trigger::cli::TriggerExecutorCommand;
Expand All @@ -24,9 +24,20 @@ use spin_trigger_http::HttpTrigger;
#[tokio::main]
async fn main() {
if let Err(err) = _main().await {
terminal::error!("{err}");
print_error_chain(err);
std::process::exit(1)
let code = match err.downcast_ref::<ExitStatusError>() {
// If we encounter an `ExitStatusError` it means a subprocess has already
// exited unsuccessfully and thus already printed error messages. No need
// to print anything additional.
Some(e) => e.code(),
// Otherwise we print the error chain.
None => {
terminal::error!("{err}");
print_error_chain(err);
1
}
};

std::process::exit(code)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl UpCommand {
if status.success() {
Ok(())
} else {
bail!(status);
Err(crate::subprocess::ExitStatusError::new(status).into())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub mod build_info;
pub mod commands;
pub(crate) mod opts;
pub mod subprocess;
mod watch_filter;
mod watch_state;

pub use crate::opts::HELP_ARGS_ONLY_TRIGGER_TYPE;
pub use opts::HELP_ARGS_ONLY_TRIGGER_TYPE;
34 changes: 34 additions & 0 deletions src/subprocess.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// An error representing a subprocess that errored
///
/// This can be used to propogate a subprocesses exit status.
/// When this error is encountered the cli will exit with the status code
/// instead of printing an error,
#[derive(Debug)]
pub struct ExitStatusError {
status: Option<i32>,
}

impl ExitStatusError {
pub(crate) fn new(status: std::process::ExitStatus) -> Self {
Self {
status: status.code(),
}
}

pub fn code(&self) -> i32 {
self.status.unwrap_or(1)
}
}

impl std::error::Error for ExitStatusError {}

impl std::fmt::Display for ExitStatusError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let _ = write!(f, "subprocess exited with status: ");
if let Some(status) = self.status {
writeln!(f, "{}", status)
} else {
writeln!(f, "unknown")
}
}
}

0 comments on commit 4be22a2

Please sign in to comment.