diff --git a/workspaces/api/apiclient/src/lib.rs b/workspaces/api/apiclient/src/lib.rs index 96e4e3d4237..90a7f842356 100644 --- a/workspaces/api/apiclient/src/lib.rs +++ b/workspaces/api/apiclient/src/lib.rs @@ -38,10 +38,12 @@ mod error { #[snafu(display("Failed to send request: {}", source))] RequestSend { source: hyper::Error }, - #[snafu(display("Status {} {} when requesting {}", code.as_u16(), code.as_str(), uri))] + #[snafu(display("Status {} when {}ing {}: {}", code.as_str(), method, uri, body))] ResponseStatus { + method: String, code: http::StatusCode, uri: http::uri::Uri, + body: String, }, #[snafu(display("Failed to read body of response: {}", source))] @@ -80,6 +82,8 @@ where S1: AsRef, S2: AsRef, { + let method = method.as_ref(); + let request_data = if let Some(data) = data { Body::from(data) } else { @@ -92,7 +96,7 @@ where let client = Client::builder().build::<_, ::hyper::Body>(UnixConnector::new()); let request = Request::builder() - .method(method.as_ref()) + .method(method) .uri(&uri) .header(header::CONTENT_TYPE, "application/json") .body(Body::from(request_data)) @@ -108,15 +112,6 @@ where .block_on(client.request(request).map(|res| res.into_parts())) .context(error::RequestSend)?; - // Error if the response status is in not in the 2xx range. - ensure!( - head.status.is_success(), - error::ResponseStatus { - code: head.status, - uri - } - ); - // Wait on the second future (the streaming body) and concatenate all the pieces together so we // have a single response body. We make sure each piece is a string, as we go; we assume that // we're not handling binary data. @@ -128,5 +123,16 @@ where .context(error::ResponseBodyRead)? .context(error::NonUtf8Response)?; + // Error if the response status is in not in the 2xx range. + ensure!( + head.status.is_success(), + error::ResponseStatus { + method, + code: head.status, + uri, + body, + } + ); + Ok((head.status, body)) } diff --git a/workspaces/api/apiclient/src/main.rs b/workspaces/api/apiclient/src/main.rs index ad5c10632a6..3587c358435 100644 --- a/workspaces/api/apiclient/src/main.rs +++ b/workspaces/api/apiclient/src/main.rs @@ -90,7 +90,7 @@ fn parse_args(args: env::Args) -> Args { } } -fn main() -> Result<(), Box> { +fn run() -> Result<(), Box> { let args = parse_args(env::args()); let (status, body) = @@ -104,3 +104,13 @@ fn main() -> Result<(), Box> { } Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/apiserver/src/bin/apiserver.rs b/workspaces/api/apiserver/src/bin/apiserver.rs index 4565a4c3434..f14dce605c5 100644 --- a/workspaces/api/apiserver/src/bin/apiserver.rs +++ b/workspaces/api/apiserver/src/bin/apiserver.rs @@ -127,7 +127,7 @@ fn parse_args(args: env::Args) -> Args { } /// Starts a web server to accept user requests, dispatching those requests to the controller. -fn main() -> Result<()> { +fn run() -> Result<()> { let args = parse_args(env::args()); // TerminalMode::Mixed will send errors to stderr and anything less to stdout. @@ -161,3 +161,13 @@ fn main() -> Result<()> { ) .context(error::Server) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/host-containers/src/main.rs b/workspaces/api/host-containers/src/main.rs index be9b24bf0ec..ecd8e9f0893 100644 --- a/workspaces/api/host-containers/src/main.rs +++ b/workspaces/api/host-containers/src/main.rs @@ -306,7 +306,7 @@ where Ok(()) } -fn main() -> Result<()> { +fn run() -> Result<()> { let args = parse_args(env::args()); // TerminalMode::Mixed will send errors to stderr and anything less to stdout. @@ -335,3 +335,13 @@ fn main() -> Result<()> { Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/migration/migrations/v0.1/borkseed/src/main.rs b/workspaces/api/migration/migrations/v0.1/borkseed/src/main.rs index 5de9440b1ba..12f77230560 100644 --- a/workspaces/api/migration/migrations/v0.1/borkseed/src/main.rs +++ b/workspaces/api/migration/migrations/v0.1/borkseed/src/main.rs @@ -2,6 +2,7 @@ use migration_helpers::{error, migrate, Migration, MigrationData, Result}; use std::convert::TryFrom; +use std::process; /// We moved from String to u32 for the seed value generated by bork and used by updog. struct BorkSeedIntMigration; @@ -79,6 +80,16 @@ impl Migration for BorkSeedIntMigration { } } -fn main() -> Result<()> { +fn run() -> Result<()> { migrate(BorkSeedIntMigration) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/migration/migrations/v0.1/host-containers-version/src/main.rs b/workspaces/api/migration/migrations/v0.1/host-containers-version/src/main.rs index 2a29c0da929..357e66e9565 100644 --- a/workspaces/api/migration/migrations/v0.1/host-containers-version/src/main.rs +++ b/workspaces/api/migration/migrations/v0.1/host-containers-version/src/main.rs @@ -1,6 +1,7 @@ #![deny(rust_2018_idioms)] use migration_helpers::{migrate, Migration, MigrationData, Result}; +use std::process; /// We bumped the versions of the default admin container and the default control container from v0.1 to v0.2 struct HostContainersVersionMigration; @@ -58,6 +59,16 @@ impl Migration for HostContainersVersionMigration { } } -fn main() -> Result<()> { +fn run() -> Result<()> { migrate(HostContainersVersionMigration) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/moondog/src/main.rs b/workspaces/api/moondog/src/main.rs index c38bd5c5922..1f13680d80c 100644 --- a/workspaces/api/moondog/src/main.rs +++ b/workspaces/api/moondog/src/main.rs @@ -279,7 +279,7 @@ fn parse_args(args: env::Args) -> Args { } } -fn main() -> Result<()> { +fn run() -> Result<()> { // Parse and store the args passed to the program let args = parse_args(env::args()); @@ -350,3 +350,13 @@ fn main() -> Result<()> { Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/pluto/src/main.rs b/workspaces/api/pluto/src/main.rs index edb9c77a17d..218662cbb09 100644 --- a/workspaces/api/pluto/src/main.rs +++ b/workspaces/api/pluto/src/main.rs @@ -227,7 +227,7 @@ fn parse_args(mut args: env::Args) -> String { args.nth(1).unwrap_or_else(|| usage()) } -fn main() -> Result<()> { +fn run() -> Result<()> { let setting_name = parse_args(env::args()); let client = reqwest::Client::new(); @@ -250,3 +250,13 @@ fn main() -> Result<()> { println!("{}", output); Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/servicedog/src/main.rs b/workspaces/api/servicedog/src/main.rs index 9fbe13ce4f8..0e750b2f858 100644 --- a/workspaces/api/servicedog/src/main.rs +++ b/workspaces/api/servicedog/src/main.rs @@ -202,22 +202,22 @@ impl SystemdUnit { /// Starts the current systemd unit with the `--no-block` option fn start_no_block(&self) -> Result<()> { - run(&["start", "--no-block", &self.unit]) + systemctl(&["start", "--no-block", &self.unit]) } /// Stops the current systemd unit fn stop(&self) -> Result<()> { - run(&["stop", &self.unit]) + systemctl(&["stop", &self.unit]) } /// Enables the current systemd unit fn enable(&self) -> Result<()> { - run(&["enable", &self.unit]) + systemctl(&["enable", &self.unit]) } /// Disables the current systemd unit fn disable(&self) -> Result<()> { - run(&["disable", &self.unit]) + systemctl(&["disable", &self.unit]) } } @@ -225,11 +225,11 @@ impl SystemdUnit { /// According to docs, this *shouldn't* be necessary but experience /// has show this isn't always the case. It shoudn't hurt to call it fn systemd_daemon_reload() -> Result<()> { - run(&["daemon-reload"]) + systemctl(&["daemon-reload"]) } /// Wrapper around process::Command that does error handling. -fn run(args: I) -> Result<()> +fn systemctl(args: I) -> Result<()> where I: IntoIterator, S: AsRef, @@ -321,7 +321,7 @@ fn parse_args(args: env::Args) -> Args { } } -fn main() -> Result<()> { +fn run() -> Result<()> { // Parse and store the args passed to the program let args = parse_args(env::args()); @@ -351,3 +351,13 @@ fn main() -> Result<()> { }; Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/settings-committer/src/main.rs b/workspaces/api/settings-committer/src/main.rs index 3ffdf252722..2eaaa71b86f 100644 --- a/workspaces/api/settings-committer/src/main.rs +++ b/workspaces/api/settings-committer/src/main.rs @@ -169,7 +169,7 @@ fn parse_args(args: env::Args) -> Args { } } -fn main() -> Result<()> { +fn run() -> Result<()> { // Parse and store the args passed to the program let args = parse_args(env::args()); @@ -185,3 +185,13 @@ fn main() -> Result<()> { Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/storewolf/src/main.rs b/workspaces/api/storewolf/src/main.rs index 2d5b76f8c06..44aedeb6461 100644 --- a/workspaces/api/storewolf/src/main.rs +++ b/workspaces/api/storewolf/src/main.rs @@ -521,7 +521,7 @@ fn parse_args(args: env::Args) -> Args { } } -fn main() -> Result<()> { +fn run() -> Result<()> { // Parse and store the args passed to the program let args = parse_args(env::args()); @@ -551,3 +551,13 @@ fn main() -> Result<()> { Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/sundog/src/main.rs b/workspaces/api/sundog/src/main.rs index e8c71763ef7..e622eb1e3f5 100644 --- a/workspaces/api/sundog/src/main.rs +++ b/workspaces/api/sundog/src/main.rs @@ -443,7 +443,7 @@ fn parse_args(args: env::Args) -> Args { } } -fn main() -> Result<()> { +fn run() -> Result<()> { // Parse and store the args passed to the program let args = parse_args(env::args()); @@ -468,3 +468,13 @@ fn main() -> Result<()> { Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +} diff --git a/workspaces/api/thar-be-settings/src/main.rs b/workspaces/api/thar-be-settings/src/main.rs index 7f3a74f00fb..585faf8b4af 100644 --- a/workspaces/api/thar-be-settings/src/main.rs +++ b/workspaces/api/thar-be-settings/src/main.rs @@ -138,7 +138,7 @@ fn write_config_files( Ok(()) } -fn main() -> Result<(), Box> { +fn run() -> Result<(), Box> { // Parse and store the args passed to the program let args = parse_args(env::args()); @@ -190,3 +190,13 @@ fn main() -> Result<(), Box> { Ok(()) } + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +}