Skip to content

Commit

Permalink
fix: handle malformed API key errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Feb 16, 2021
1 parent 25d4ba9 commit 0249bcd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/rover-client/src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions crates/rover-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/error/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/error/metadata/suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum Suggestion {
ProvideValidSubgraph(Vec<String>),
Adhoc(String),
CheckKey,
ProperKey,
}

impl Display for Suggestion {
Expand Down Expand Up @@ -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()

};
Expand Down

0 comments on commit 0249bcd

Please sign in to comment.