Skip to content

Commit 4bc971d

Browse files
feat(rover): scaffolds app-level error codes and suggestions
1 parent 4af6fb5 commit 4bc971d

30 files changed

+209
-62
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ robot-panic = { path = "./crates/robot-panic" }
1616
binstall = { path = "./installers/binstall" }
1717

1818
# crates.io deps
19-
anyhow = "1.0.36"
19+
anyhow = "1.0.38"
2020
atty = "0.2.14"
21+
ansi_term = "0.12.1"
2122
console = "0.14.0"
2223
heck = "0.3.2"
2324
prettytable-rs = "0.8.0"

src/bin/rover.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
use anyhow::Result;
1+
use command::RoverStdout;
22
use robot_panic::setup_panic;
33
use rover::*;
44
use sputnik::Session;
55
use structopt::StructOpt;
66

7-
use std::thread;
7+
use std::{process, thread};
88

9-
fn main() -> Result<()> {
9+
fn main() {
1010
setup_panic!();
11+
if let Err(error) = run() {
12+
tracing::debug!(?error);
13+
eprintln!("{}", error);
14+
process::exit(1)
15+
} else {
16+
process::exit(0)
17+
}
18+
}
1119

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

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

48-
result.print();
57+
output.print();
4958
Ok(())
5059
}

src/cli.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use anyhow::Result;
21
use serde::Serialize;
32
use structopt::StructOpt;
43

54
use crate::env::{RoverEnv, RoverEnvKey};
65
use crate::stringify::from_display;
6+
use crate::Result;
77
use crate::{
88
client::StudioClientConfig,
99
command::{self, RoverStdout},

src/client.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use anyhow::Result;
1+
use crate::Result;
2+
23
use houston as config;
34
use rover_client::blocking::StudioClient;
45

src/command/config/clear.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use anyhow::{Context, Result};
21
use serde::Serialize;
32
use structopt::StructOpt;
43

54
use crate::command::RoverStdout;
5+
use crate::{Context, Result};
66

77
use houston as config;
88

src/command/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
mod clear;
22
mod profile;
33

4-
use anyhow::Result;
54
use serde::Serialize;
65
use structopt::StructOpt;
76

87
use houston as config;
98

109
use crate::command::RoverStdout;
10+
use crate::Result;
1111

1212
#[derive(Debug, Serialize, StructOpt)]
1313
pub struct Config {

src/command/config/profile/auth.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anyhow::{Context, Error, Result};
21
use console::{self, style};
32
use serde::Serialize;
43
use structopt::StructOpt;
@@ -7,6 +6,7 @@ use config::Profile;
76
use houston as config;
87

98
use crate::command::RoverStdout;
9+
use crate::{anyhow, Context, Result};
1010

1111
#[derive(Debug, Serialize, StructOpt)]
1212
/// Set a configuration profile's Apollo Studio API key
@@ -25,7 +25,7 @@ pub struct Auth {
2525

2626
impl Auth {
2727
pub fn run(&self, config: config::Config) -> Result<RoverStdout> {
28-
let api_key = api_key_prompt().context("Failed to read API key from terminal")?;
28+
let api_key = api_key_prompt()?;
2929
Profile::set_api_key(&self.profile_name, &config, &api_key)
3030
.context("Failed while saving API key")?;
3131
Profile::get_api_key(&self.profile_name, &config)
@@ -48,7 +48,7 @@ fn api_key_prompt() -> Result<String> {
4848
if is_valid(&api_key) {
4949
Ok(api_key)
5050
} else {
51-
Err(Error::msg("Received an empty API Key. Please try again."))
51+
Err(anyhow!("Received an empty API Key. Please try again.").into())
5252
}
5353
}
5454

src/command/config/profile/delete.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use anyhow::{Context, Result};
21
use serde::Serialize;
32
use structopt::StructOpt;
43

54
use houston as config;
65

76
use crate::command::RoverStdout;
7+
use crate::{Context, Result};
88

99
#[derive(Debug, Serialize, StructOpt)]
1010
/// Delete a configuration profile

src/command/config/profile/list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use anyhow::{Context, Result};
21
use serde::Serialize;
32
use structopt::StructOpt;
43

4+
use crate::{Context, Result};
55
use houston as config;
66

77
use crate::command::RoverStdout;

src/command/config/profile/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ mod delete;
33
mod list;
44
mod show;
55

6-
use anyhow::Result;
76
use serde::Serialize;
87
use structopt::StructOpt;
98

109
use crate::command::RoverStdout;
10+
use crate::Result;
1111

1212
use houston as config;
1313

src/command/config/profile/show.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use anyhow::Result;
21
use serde::Serialize;
32
use structopt::StructOpt;
43

54
use houston as config;
65

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

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

3729
tracing::info!("{}: {}", &self.name, profile);
3830
Ok(RoverStdout::None)

src/command/graph/check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anyhow::{Context, Result};
21
use prettytable::{cell, row, Table};
32
use serde::Serialize;
43
use structopt::StructOpt;
@@ -9,6 +8,7 @@ use crate::client::StudioClientConfig;
98
use crate::command::RoverStdout;
109
use crate::utils::loaders::load_schema_from_flag;
1110
use crate::utils::parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource};
11+
use crate::{Context, Result};
1212

1313
#[derive(Debug, Serialize, StructOpt)]
1414
pub struct Check {
@@ -68,8 +68,8 @@ impl Check {
6868

6969
match num_failures {
7070
0 => Ok(RoverStdout::None),
71-
1 => Err(anyhow::anyhow!("Encountered 1 failure.")),
72-
_ => Err(anyhow::anyhow!("Encountered {} failures.", num_failures)),
71+
1 => Err(anyhow::anyhow!("Encountered 1 failure.").into()),
72+
_ => Err(anyhow::anyhow!("Encountered {} failures.", num_failures).into()),
7373
}
7474
}
7575
}

src/command/graph/fetch.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anyhow::{Context, Result};
21
use serde::Serialize;
32
use structopt::StructOpt;
43

@@ -7,6 +6,7 @@ use rover_client::query::graph::fetch;
76
use crate::client::StudioClientConfig;
87
use crate::command::RoverStdout;
98
use crate::utils::parsers::{parse_graph_ref, GraphRef};
9+
use crate::Result;
1010

1111
#[derive(Debug, Serialize, StructOpt)]
1212
pub struct Fetch {
@@ -39,8 +39,7 @@ impl Fetch {
3939
variant: Some(self.graph.variant.clone()),
4040
},
4141
&client,
42-
)
43-
.context("Failed while fetching from Apollo Studio")?;
42+
)?;
4443

4544
Ok(RoverStdout::SDL(sdl))
4645
}

src/command/graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ mod check;
22
mod fetch;
33
mod push;
44

5-
use anyhow::Result;
65
use serde::Serialize;
76
use structopt::StructOpt;
87

98
use crate::client::StudioClientConfig;
109
use crate::command::RoverStdout;
10+
use crate::Result;
1111

1212
#[derive(Debug, Serialize, StructOpt)]
1313
pub struct Graph {

src/command/graph/push.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anyhow::{Context, Result};
21
use serde::Serialize;
32
use structopt::StructOpt;
43

@@ -8,6 +7,7 @@ use crate::client::StudioClientConfig;
87
use crate::command::RoverStdout;
98
use crate::utils::loaders::load_schema_from_flag;
109
use crate::utils::parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource};
10+
use crate::{Context, Result};
1111

1212
#[derive(Debug, Serialize, StructOpt)]
1313
pub struct Push {

src/command/install/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use anyhow::{anyhow, Context, Result};
21
use serde::Serialize;
32
use structopt::StructOpt;
43

54
use binstall::Installer;
65

76
use crate::command::RoverStdout;
7+
use crate::{anyhow, Context, Result};
88

99
use std::env;
1010
use std::path::PathBuf;
@@ -36,7 +36,7 @@ impl Install {
3636
}
3737
Ok(RoverStdout::None)
3838
} else {
39-
Err(anyhow!("Failed to get the current executable's path."))
39+
Err(anyhow!("Failed to get the current executable's path.").into())
4040
}
4141
}
4242
}

src/command/subgraph/check.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use anyhow::{Context, Result};
21
use prettytable::{cell, row, Table};
32
use serde::Serialize;
43
use structopt::StructOpt;
54

5+
use crate::{Context, Result};
66
use rover_client::query::subgraph::check;
77

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

114114
match num_failures {
115115
0 => Ok(RoverStdout::None),
116-
1 => Err(anyhow::anyhow!(
117-
"Encountered 1 failure while checking your subgraph."
118-
)),
116+
1 => Err(anyhow::anyhow!("Encountered 1 failure while checking your subgraph.").into()),
119117
_ => Err(anyhow::anyhow!(
120118
"Encountered {} failures while checking your subgraph.",
121119
num_failures
122-
)),
120+
)
121+
.into()),
123122
}
124123
}
125124

@@ -133,12 +132,13 @@ fn handle_composition_errors(
133132
}
134133
match num_failures {
135134
0 => Ok(RoverStdout::None),
136-
1 => Err(anyhow::anyhow!(
137-
"Encountered 1 composition error while composing the subgraph."
138-
)),
135+
1 => Err(
136+
anyhow::anyhow!("Encountered 1 composition error while composing the subgraph.").into(),
137+
),
139138
_ => Err(anyhow::anyhow!(
140139
"Encountered {} composition errors while composing the subgraph.",
141140
num_failures
142-
)),
141+
)
142+
.into()),
143143
}
144144
}

src/command/subgraph/delete.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::client::StudioClientConfig;
22
use crate::command::RoverStdout;
33
use crate::utils::parsers::{parse_graph_ref, GraphRef};
4-
use anyhow::Result;
4+
use crate::Result;
5+
56
use rover_client::query::subgraph::delete::{self, DeleteServiceResponse};
7+
68
use serde::Serialize;
79
use structopt::StructOpt;
810

src/command/subgraph/fetch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anyhow::{Context, Result};
21
use serde::Serialize;
32
use structopt::StructOpt;
43

@@ -7,6 +6,7 @@ use rover_client::query::subgraph::fetch;
76
use crate::client::StudioClientConfig;
87
use crate::command::RoverStdout;
98
use crate::utils::parsers::{parse_graph_ref, GraphRef};
9+
use crate::{Context, Result};
1010

1111
#[derive(Debug, Serialize, StructOpt)]
1212
pub struct Fetch {

src/command/subgraph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ mod delete;
33
mod fetch;
44
mod push;
55

6-
use anyhow::Result;
76
use serde::Serialize;
87
use structopt::StructOpt;
98

109
use crate::client::StudioClientConfig;
1110
use crate::command::RoverStdout;
11+
use crate::Result;
1212

1313
#[derive(Debug, Serialize, StructOpt)]
1414
pub struct Subgraph {

src/command/subgraph/push.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use anyhow::{Context, Result};
2-
use rover_client::query::subgraph::push::{self, PushPartialSchemaResponse};
31
use serde::Serialize;
42
use structopt::StructOpt;
53

64
use crate::client::StudioClientConfig;
75
use crate::command::RoverStdout;
8-
96
use crate::utils::loaders::load_schema_from_flag;
107
use crate::utils::parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource};
8+
use crate::{Context, Result};
9+
10+
use rover_client::query::subgraph::push::{self, PushPartialSchemaResponse};
1111

1212
#[derive(Debug, Serialize, StructOpt)]
1313
pub struct Push {

0 commit comments

Comments
 (0)