From 8b0bea04abbc916eb9ebb20cc3f6e8d5449a7238 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Thu, 6 May 2021 16:01:52 -0500 Subject: [PATCH] feat: RoverClientError::CouldNotConnect --- CHANGELOG.md | 8 ++++++++ crates/rover-client/src/blocking/client.rs | 12 +++++++++++- crates/rover-client/src/error.rs | 21 +++++++++++++++++---- src/error/metadata/mod.rs | 5 ++++- src/error/metadata/suggestion.rs | 2 ++ 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 233af15f60..45039f7140 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm [pull/484]: https://github.com/apollographql/rover/pull/484 [issue/169]: https://github.com/apollographql/rover/issues/169 +- **Better error messages for HTTP errors - [EverlastingBugstopper], [issue/489] [pull/518]** + + Previously, Rover obfuscated the information about HTTP errors that occurred. Now, if something goes wrong between your machine and any HTTP server, you'll get some more information about what exactly went wrong. + + [Author]: https://github.com/EverlastingBugstopper + [pull/PR #]: https://github.com/apollographql/rover/pull/518 + [issue/Issue #]: https://github.com/apollographql/rover/issues/489 + ## 🐛 Fixes ## 🛠 Maintenance diff --git a/crates/rover-client/src/blocking/client.rs b/crates/rover-client/src/blocking/client.rs index 559fc8abd9..69d3b4a642 100644 --- a/crates/rover-client/src/blocking/client.rs +++ b/crates/rover-client/src/blocking/client.rs @@ -40,7 +40,17 @@ impl Client { .post(&self.uri) .headers(h) .json(&body) - .send()? + .send() + .map_err(|e| { + if e.is_connect() { + RoverClientError::CouldNotConnect { + url: e.url().cloned(), + source: e, + } + } else { + e.into() + } + })? .error_for_status()?; Client::handle_response::(response) diff --git a/crates/rover-client/src/error.rs b/crates/rover-client/src/error.rs index 477ff76c4d..0985e1cda2 100644 --- a/crates/rover-client/src/error.rs +++ b/crates/rover-client/src/error.rs @@ -1,3 +1,4 @@ +use reqwest::Url; use thiserror::Error; /// RoverClientError represents all possible failures that can occur during a client request. @@ -18,15 +19,15 @@ pub enum RoverClientError { }, /// Tried to build a [HeaderMap] with an invalid header name. - #[error("invalid header name")] + #[error("Invalid header name")] InvalidHeaderName(#[from] reqwest::header::InvalidHeaderName), /// Tried to build a [HeaderMap] with an invalid header value. - #[error("invalid header value")] + #[error("Invalid header value")] InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue), /// Invalid JSON in response body. - #[error("could not parse JSON")] + #[error("Could not parse JSON")] InvalidJson(#[from] serde_json::Error), /// Encountered an error handling the received response. @@ -73,8 +74,20 @@ pub enum RoverClientError { frontend_url_root: String, }, + #[error("Could not connect to {}.", + if let Some(url) = .url { + url.to_string() + } else { + "Unknown URL".to_string() + } + )] + CouldNotConnect { + source: reqwest::Error, + url: Option, + }, + /// Encountered an error sending the request. - #[error("encountered an error while sending a request")] + #[error(transparent)] SendRequest(#[from] reqwest::Error), /// when someone provides a bad graph/variant combination or isn't diff --git a/src/error/metadata/mod.rs b/src/error/metadata/mod.rs index 1e147d20bc..3d980539fe 100644 --- a/src/error/metadata/mod.rs +++ b/src/error/metadata/mod.rs @@ -43,9 +43,12 @@ impl From<&mut anyhow::Error> for Metadata { RoverClientError::InvalidJson(_) | RoverClientError::InvalidHeaderName(_) | RoverClientError::InvalidHeaderValue(_) - | RoverClientError::SendRequest(_) | RoverClientError::MalformedResponse { null_field: _ } | RoverClientError::InvalidSeverity => (Some(Suggestion::SubmitIssue), None), + RoverClientError::SendRequest(_) => (None, None), + RoverClientError::CouldNotConnect { .. } => { + (Some(Suggestion::CheckServerConnection), None) + } RoverClientError::NoCompositionPublishes { graph: _, composition_errors, diff --git a/src/error/metadata/suggestion.rs b/src/error/metadata/suggestion.rs index 1a2c898e67..3d4dcae9e2 100644 --- a/src/error/metadata/suggestion.rs +++ b/src/error/metadata/suggestion.rs @@ -27,6 +27,7 @@ pub enum Suggestion { CheckKey, ProperKey, NewUserNoProfiles, + CheckServerConnection, } impl Display for Suggestion { @@ -115,6 +116,7 @@ impl Display for Suggestion { ) } Suggestion::Adhoc(msg) => msg.to_string(), + Suggestion::CheckServerConnection => "Make sure the endpoint is accepting connections.".to_string() };