From 0249bcd250a2ce9abf5d9bdf2b8e5fa3b9f42193 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Tue, 16 Feb 2021 15:27:41 -0600 Subject: [PATCH] fix: handle malformed API key errors --- crates/rover-client/src/blocking/client.rs | 4 ++++ crates/rover-client/src/error.rs | 6 ++++++ src/error/metadata/mod.rs | 1 + src/error/metadata/suggestion.rs | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/crates/rover-client/src/blocking/client.rs b/crates/rover-client/src/blocking/client.rs index 7ac330020..096169de0 100644 --- a/crates/rover-client/src/blocking/client.rs +++ b/crates/rover-client/src/blocking/client.rs @@ -55,6 +55,10 @@ impl Client { serde_json::from_str(&response_text)?; if let Some(errs) = response_body.errors { + if !errs.is_empty() && errs[0].message.contains("406") { + return Err(RoverClientError::MalformedKey); + } + return Err(RoverClientError::GraphQL { msg: errs .into_iter() diff --git a/crates/rover-client/src/error.rs b/crates/rover-client/src/error.rs index ad31a3241..d4d6dbd08 100644 --- a/crates/rover-client/src/error.rs +++ b/crates/rover-client/src/error.rs @@ -75,6 +75,12 @@ pub enum RoverClientError { #[error("Invalid ChangeSeverity.")] InvalidSeverity, + /// This error occurs when a user has a malformed API key + #[error( + "The API key you provided is malformed. An API key must have three parts separated by a colon." + )] + MalformedKey, + /// The registry could not find this key #[error("The registry did not recognize the provided API key")] InvalidKey, diff --git a/src/error/metadata/mod.rs b/src/error/metadata/mod.rs index 704965f06..ae397e1b2 100644 --- a/src/error/metadata/mod.rs +++ b/src/error/metadata/mod.rs @@ -70,6 +70,7 @@ impl From<&mut anyhow::Error> for Metadata { (None, None) } RoverClientError::InvalidKey => (Some(Suggestion::CheckKey), None), + RoverClientError::MalformedKey => (Some(Suggestion::ProperKey), None), }; return Metadata { suggestion, diff --git a/src/error/metadata/suggestion.rs b/src/error/metadata/suggestion.rs index bcc57b4bf..04fccd00c 100644 --- a/src/error/metadata/suggestion.rs +++ b/src/error/metadata/suggestion.rs @@ -19,6 +19,7 @@ pub enum Suggestion { ProvideValidSubgraph(Vec), Adhoc(String), CheckKey, + ProperKey, } impl Display for Suggestion { @@ -79,6 +80,9 @@ impl Display for Suggestion { Suggestion::CheckKey => { "Check your API key to make sure it's valid (are you using the right profile?).".to_string() } + Suggestion::ProperKey => { + format!("Visit {} for more details on Apollo's API keys.", Cyan.normal().paint("https://go.apollo.dev/r/api-keys")) + } Suggestion::Adhoc(msg) => msg.to_string() };