Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Handle failed exit codes elegantly #799

Merged
merged 4 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ pub fn run_generate(name: &str, template: &str) -> Result<(), failure::Error> {
let command = command(name, binary_path, &args);
let command_name = format!("{:?}", command);

commands::run(command, &command_name)?;
Ok(())
commands::run(command, &command_name)
EverlastingBugstopper marked this conversation as resolved.
Show resolved Hide resolved
}

fn command(name: &str, binary_path: PathBuf, args: &[&str]) -> Command {
Expand Down
33 changes: 23 additions & 10 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::process::Command;

use log::info;

pub mod build;
pub mod config;
pub mod generate;
Expand All @@ -24,21 +22,36 @@ pub use subdomain::get_subdomain;
pub use subdomain::set_subdomain;
pub use whoami::whoami;

use std::str;
EverlastingBugstopper marked this conversation as resolved.
Show resolved Hide resolved

const UNKNOWN_ERR: &str =
"An unexpected error occurred, try running the command again with RUSTLOG=info";

/// Run the given command and return its stdout.
pub fn run(mut command: Command, command_name: &str) -> Result<(), failure::Error> {
info!("Running {:?}", command);
log::info!("Running {:?}", command);

let status = command.status()?;
let output = command.output()?;
dbg!(output.clone());
EverlastingBugstopper marked this conversation as resolved.
Show resolved Hide resolved

if status.success() {
Ok(())
} else {
failure::bail!(
println!(
"{}",
String::from_utf8(output.stdout).expect(UNKNOWN_ERR).trim()
);

if !output.status.success() {
log::info!(
"failed to execute `{}`: exited with {}",
command_name,
status
)
output.status
);
let mut serr = String::from_utf8(output.stderr).expect(UNKNOWN_ERR);
if serr.starts_with("Error: ") {
serr = serr.get(7..).unwrap_or(UNKNOWN_ERR).to_string();
}
failure::bail!("{}", serr.trim())
}
Ok(())
EverlastingBugstopper marked this conversation as resolved.
Show resolved Hide resolved
}

// Ensures that Worker name is valid.
Expand Down