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

Application level error consumption and handling #186

Merged
merged 1 commit into from
Jan 27, 2021
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ robot-panic = { path = "./crates/robot-panic" }
binstall = { path = "./installers/binstall" }

# crates.io deps
anyhow = "1.0.36"
anyhow = "1.0.38"
atty = "0.2.14"
ansi_term = "0.12.1"
console = "0.14.0"
heck = "0.3.2"
prettytable-rs = "0.8.0"
Expand Down
19 changes: 14 additions & 5 deletions src/bin/rover.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use anyhow::Result;
use command::RoverStdout;
use robot_panic::setup_panic;
use rover::*;
use sputnik::Session;
use structopt::StructOpt;

use std::thread;
use std::{process, thread};

fn main() -> Result<()> {
fn main() {
setup_panic!();
if let Err(error) = run() {
tracing::debug!(?error);
eprintln!("{}", error);
process::exit(1)
} else {
process::exit(0)
}
}

fn run() -> Result<()> {
let app = cli::Rover::from_args();
timber::init(app.log_level);
tracing::trace!(command_structure = ?app);

// attempt to create a new `Session` to capture anonymous usage data
let result = match Session::new(&app) {
let output: RoverStdout = match Session::new(&app) {
// if successful, report the usage data in the background
Ok(session) => {
// kicks off the reporting on a background thread
Expand Down Expand Up @@ -45,6 +54,6 @@ fn main() -> Result<()> {
Err(_) => app.run(),
}?;

result.print();
output.print();
Ok(())
}
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use serde::Serialize;
use structopt::StructOpt;

use crate::env::{RoverEnv, RoverEnvKey};
use crate::stringify::from_display;
use crate::Result;
use crate::{
client::StudioClientConfig,
command::{self, RoverStdout},
Expand Down
3 changes: 2 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use crate::Result;

use houston as config;
use rover_client::blocking::StudioClient;

Expand Down
6 changes: 3 additions & 3 deletions src/command/config/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::{Context, Error, Result};
use console::{self, style};
use serde::Serialize;
use structopt::StructOpt;
Expand All @@ -7,6 +6,7 @@ use config::Profile;
use houston as config;

use crate::command::RoverStdout;
use crate::{anyhow, Context, Result};

#[derive(Debug, Serialize, StructOpt)]
/// Authenticate a configuration profile with an API key
Expand All @@ -27,7 +27,7 @@ pub struct Auth {

impl Auth {
pub fn run(&self, config: config::Config) -> Result<RoverStdout> {
let api_key = api_key_prompt().context("Failed to read API key from terminal")?;
let api_key = api_key_prompt()?;
Profile::set_api_key(&self.profile_name, &config, &api_key)
.context("Failed while saving API key")?;
Profile::get_api_key(&self.profile_name, &config)
Expand All @@ -50,7 +50,7 @@ fn api_key_prompt() -> Result<String> {
if is_valid(&api_key) {
Ok(api_key)
} else {
Err(Error::msg("Received an empty API Key. Please try again."))
Err(anyhow!("Received an empty API Key. Please try again.").into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/command/config/clear.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{Context, Result};
use serde::Serialize;
use structopt::StructOpt;

use crate::command::RoverStdout;
use crate::{Context, Result};

use houston as config;

Expand Down
2 changes: 1 addition & 1 deletion src/command/config/delete.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::{Context, Result};
use serde::Serialize;
use structopt::StructOpt;

use houston as config;

use crate::command::RoverStdout;
use crate::{Context, Result};

#[derive(Debug, Serialize, StructOpt)]
/// Delete a configuration profile
Expand Down
2 changes: 1 addition & 1 deletion src/command/config/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{Context, Result};
use serde::Serialize;
use structopt::StructOpt;

use crate::{Context, Result};
use houston as config;

use crate::command::RoverStdout;
Expand Down
2 changes: 1 addition & 1 deletion src/command/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ mod delete;
mod list;
mod show;

use anyhow::Result;
use serde::Serialize;
use structopt::StructOpt;

use houston as config;

use crate::command::RoverStdout;
use crate::Result;

#[derive(Debug, Serialize, StructOpt)]
pub struct Config {
Expand Down
12 changes: 2 additions & 10 deletions src/command/config/show.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use serde::Serialize;
use structopt::StructOpt;

use houston as config;

use crate::command::RoverStdout;
use crate::Result;
#[derive(Debug, Serialize, StructOpt)]
/// View a configuration profile's details
///
Expand All @@ -24,15 +24,7 @@ impl Show {
sensitive: self.sensitive,
};

let profile = config::Profile::load(&self.name, &config, opts).map_err(|e| {
let context = match e {
config::HoustonProblem::NoNonSensitiveConfigFound(_) => {
"Could not show any profile information. Try re-running with the `--sensitive` flag"
}
_ => "Could not load profile",
};
anyhow::anyhow!(e).context(context)
})?;
let profile = config::Profile::load(&self.name, &config, opts)?;

tracing::info!("{}: {}", &self.name, profile);
Ok(RoverStdout::None)
Expand Down
6 changes: 3 additions & 3 deletions src/command/graph/check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::{Context, Result};
use prettytable::{cell, row, Table};
use serde::Serialize;
use structopt::StructOpt;
Expand All @@ -9,6 +8,7 @@ use crate::client::StudioClientConfig;
use crate::command::RoverStdout;
use crate::utils::loaders::load_schema_from_flag;
use crate::utils::parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource};
use crate::{Context, Result};

#[derive(Debug, Serialize, StructOpt)]
pub struct Check {
Expand Down Expand Up @@ -68,8 +68,8 @@ impl Check {

match num_failures {
0 => Ok(RoverStdout::None),
1 => Err(anyhow::anyhow!("Encountered 1 failure.")),
_ => Err(anyhow::anyhow!("Encountered {} failures.", num_failures)),
1 => Err(anyhow::anyhow!("Encountered 1 failure.").into()),
_ => Err(anyhow::anyhow!("Encountered {} failures.", num_failures).into()),
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/command/graph/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::{Context, Result};
use serde::Serialize;
use structopt::StructOpt;

Expand All @@ -7,6 +6,7 @@ use rover_client::query::graph::fetch;
use crate::client::StudioClientConfig;
use crate::command::RoverStdout;
use crate::utils::parsers::{parse_graph_ref, GraphRef};
use crate::Result;

#[derive(Debug, Serialize, StructOpt)]
pub struct Fetch {
Expand Down Expand Up @@ -39,8 +39,7 @@ impl Fetch {
variant: Some(self.graph.variant.clone()),
},
&client,
)
.context("Failed while fetching from Apollo Studio")?;
)?;

Ok(RoverStdout::SDL(sdl))
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ mod check;
mod fetch;
mod push;

use anyhow::Result;
use serde::Serialize;
use structopt::StructOpt;

use crate::client::StudioClientConfig;
use crate::command::RoverStdout;
use crate::Result;

#[derive(Debug, Serialize, StructOpt)]
pub struct Graph {
Expand Down
2 changes: 1 addition & 1 deletion src/command/graph/push.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::{Context, Result};
use serde::Serialize;
use structopt::StructOpt;

Expand All @@ -8,6 +7,7 @@ use crate::client::StudioClientConfig;
use crate::command::RoverStdout;
use crate::utils::loaders::load_schema_from_flag;
use crate::utils::parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource};
use crate::{Context, Result};

#[derive(Debug, Serialize, StructOpt)]
pub struct Push {
Expand Down
4 changes: 2 additions & 2 deletions src/command/install/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::{anyhow, Context, Result};
use serde::Serialize;
use structopt::StructOpt;

use binstall::Installer;

use crate::command::RoverStdout;
use crate::{anyhow, Context, Result};

use std::env;
use std::path::PathBuf;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl Install {
}
Ok(RoverStdout::None)
} else {
Err(anyhow!("Failed to get the current executable's path."))
Err(anyhow!("Failed to get the current executable's path.").into())
}
}
}
18 changes: 9 additions & 9 deletions src/command/subgraph/check.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{Context, Result};
use prettytable::{cell, row, Table};
use serde::Serialize;
use structopt::StructOpt;

use crate::{Context, Result};
use rover_client::query::subgraph::check;

use crate::client::StudioClientConfig;
Expand Down Expand Up @@ -113,13 +113,12 @@ fn handle_checks(check_result: check::CheckResult) -> Result<RoverStdout> {

match num_failures {
0 => Ok(RoverStdout::None),
1 => Err(anyhow::anyhow!(
"Encountered 1 failure while checking your subgraph."
)),
1 => Err(anyhow::anyhow!("Encountered 1 failure while checking your subgraph.").into()),
_ => Err(anyhow::anyhow!(
"Encountered {} failures while checking your subgraph.",
num_failures
)),
)
.into()),
}
}

Expand All @@ -133,12 +132,13 @@ fn handle_composition_errors(
}
match num_failures {
0 => Ok(RoverStdout::None),
1 => Err(anyhow::anyhow!(
"Encountered 1 composition error while composing the subgraph."
)),
1 => Err(
anyhow::anyhow!("Encountered 1 composition error while composing the subgraph.").into(),
),
_ => Err(anyhow::anyhow!(
"Encountered {} composition errors while composing the subgraph.",
num_failures
)),
)
.into()),
}
}
4 changes: 3 additions & 1 deletion src/command/subgraph/delete.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::client::StudioClientConfig;
use crate::command::RoverStdout;
use crate::utils::parsers::{parse_graph_ref, GraphRef};
use anyhow::Result;
use crate::Result;

use rover_client::query::subgraph::delete::{self, DeleteServiceResponse};

use serde::Serialize;
use structopt::StructOpt;

Expand Down
2 changes: 1 addition & 1 deletion src/command/subgraph/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::{Context, Result};
use serde::Serialize;
use structopt::StructOpt;

Expand All @@ -7,6 +6,7 @@ use rover_client::query::subgraph::fetch;
use crate::client::StudioClientConfig;
use crate::command::RoverStdout;
use crate::utils::parsers::{parse_graph_ref, GraphRef};
use crate::{Context, Result};

#[derive(Debug, Serialize, StructOpt)]
pub struct Fetch {
Expand Down
2 changes: 1 addition & 1 deletion src/command/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ mod delete;
mod fetch;
mod push;

use anyhow::Result;
use serde::Serialize;
use structopt::StructOpt;

use crate::client::StudioClientConfig;
use crate::command::RoverStdout;
use crate::Result;

#[derive(Debug, Serialize, StructOpt)]
pub struct Subgraph {
Expand Down
6 changes: 3 additions & 3 deletions src/command/subgraph/push.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use anyhow::{Context, Result};
use rover_client::query::subgraph::push::{self, PushPartialSchemaResponse};
use serde::Serialize;
use structopt::StructOpt;

use crate::client::StudioClientConfig;
use crate::command::RoverStdout;

use crate::utils::loaders::load_schema_from_flag;
use crate::utils::parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource};
use crate::{Context, Result};

use rover_client::query::subgraph::push::{self, PushPartialSchemaResponse};

#[derive(Debug, Serialize, StructOpt)]
pub struct Push {
Expand Down
10 changes: 10 additions & 0 deletions src/error/metadata/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::fmt::{self, Display};

#[derive(Debug)]
pub enum Code {}

impl Display for Code {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{:?}", &self)
}
}
Loading