Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print Display rather than Debug forms of errors #546

Merged
merged 1 commit into from
Nov 21, 2019
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
28 changes: 17 additions & 11 deletions workspaces/api/apiclient/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -80,6 +82,8 @@ where
S1: AsRef<str>,
S2: AsRef<str>,
{
let method = method.as_ref();

let request_data = if let Some(data) = data {
Body::from(data)
} else {
Expand All @@ -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))
Expand All @@ -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.
Expand All @@ -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))
}
12 changes: 11 additions & 1 deletion workspaces/api/apiclient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn parse_args(args: env::Args) -> Args {
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn run() -> Result<(), Box<dyn std::error::Error>> {
let args = parse_args(env::args());

let (status, body) =
Expand All @@ -104,3 +104,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/apiserver/src/bin/apiserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/host-containers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
13 changes: 12 additions & 1 deletion workspaces/api/migration/migrations/v0.1/borkseed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/moondog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/pluto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}
24 changes: 17 additions & 7 deletions workspaces/api/servicedog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,34 @@ 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])
}
}

/// Calls `systemd daemon-reload` which reloads the systemd configuration.
/// 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<I, S>(args: I) -> Result<()>
fn systemctl<I, S>(args: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/settings-committer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/storewolf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/sundog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
}
}
12 changes: 11 additions & 1 deletion workspaces/api/thar-be-settings/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn write_config_files(
Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn run() -> Result<(), Box<dyn std::error::Error>> {
// Parse and store the args passed to the program
let args = parse_args(env::args());

Expand Down Expand Up @@ -190,3 +190,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

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);
}
}