Skip to content

Commit

Permalink
Improve http errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 27, 2023
1 parent 1204042 commit 3b150e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 3 additions & 1 deletion crates/warpgate/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ pub enum WarpgateError {
Serde(String),

#[diagnostic(code(plugin::http))]
#[error("Failed to make HTTP request.")]
#[error("Failed to make HTTP request for {}.", .url.style(Style::Url))]
Http {
url: String,

#[source]
error: reqwest::Error,
},
Expand Down
10 changes: 8 additions & 2 deletions crates/warpgate/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ pub async fn download_from_url_to_file(
.get(url)
.send()
.await
.map_err(|error| WarpgateError::Http { error })?;
.map_err(|error| WarpgateError::Http {
error,
url: source_url.to_owned(),
})?;
let status = response.status();

if status.as_u16() == 404 {
Expand All @@ -73,7 +76,10 @@ pub async fn download_from_url_to_file(
response
.bytes()
.await
.map_err(|error| WarpgateError::Http { error })?,
.map_err(|error| WarpgateError::Http {
error,
url: source_url.to_owned(),
})?,
)?;

Ok(())
Expand Down
33 changes: 18 additions & 15 deletions crates/warpgate/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,21 @@ impl PluginLoader {
"Attempting to download plugin from GitHub release",
);

let handle_error = |error: reqwest::Error| WarpgateError::Http {
error,
url: api_url.clone(),
};

// Otherwise make an HTTP request to the GitHub releases API,
// and loop through the assets to find a matching one.
let mut request = client.get(api_url);
let mut request = client.get(&api_url);

if let Ok(auth_token) = env::var("GITHUB_TOKEN") {
request = request.bearer_auth(auth_token);
}

let response = request
.send()
.await
.map_err(|error| WarpgateError::Http { error })?;
let release: GitHubApiRelease = response
.json()
.await
.map_err(|error| WarpgateError::Http { error })?;
let response = request.send().await.map_err(handle_error)?;
let release: GitHubApiRelease = response.json().await.map_err(handle_error)?;

// Find a direct WASM asset first
for asset in &release.assets {
Expand Down Expand Up @@ -322,8 +321,15 @@ impl PluginLoader {
);

// Otherwise make a GraphQL request to the WAPM registry API.
let url = "https://registry.wapm.io/graphql".to_owned();

let handle_error = |error: reqwest::Error| WarpgateError::Http {
error,
url: url.clone(),
};

let response = client
.post("https://registry.wapm.io/graphql")
.post(&url)
.json(&WapmPackageRequest {
query: WAPM_GQL_QUERY.to_owned(),
variables: WapmPackageRequestVariables {
Expand All @@ -334,12 +340,9 @@ impl PluginLoader {
})
.send()
.await
.map_err(|error| WarpgateError::Http { error })?;
.map_err(handle_error)?;

let package: WapmPackageResponse = response
.json()
.await
.map_err(|error| WarpgateError::Http { error })?;
let package: WapmPackageResponse = response.json().await.map_err(handle_error)?;
let package = package.data.package_version;

// Check modules first for a direct WASM file to use
Expand Down

0 comments on commit 3b150e7

Please sign in to comment.