Skip to content

Commit

Permalink
Show more details for network errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
awaitlink committed Jul 24, 2023
1 parent 8c98a9b commit dbb2dc7
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use anyhow::{anyhow, Context};
use anyhow::{anyhow, bail, Context};
use serde::de::DeserializeOwned;
use worker::{wasm_bindgen::JsValue, Fetch, Headers, Method, Request, RequestInit, Response, Url};

Expand All @@ -22,11 +22,25 @@ pub async fn fetch(configuration: Fetch) -> anyhow::Result<Response> {
.map_err(|e| anyhow!(e.to_string()))
.context("could not fetch");

if let Ok(response) = &result {
if let Ok(mut response) = result {
tracing::debug!(response.status_code = response.status_code());
}

result
if response.status_code() < 200 || response.status_code() > 299 {
let text = response
.text()
.await
.map_err(|e| anyhow!(e.to_string()))
.context("could not get text of response")?;

tracing::debug!(response.text = text);

bail!("status code not 2xx: {}", response.status_code());
} else {
Ok(response)
}
} else {
result
}
}

pub async fn json_from_response<T: DeserializeOwned>(response: &mut Response) -> anyhow::Result<T> {
Expand Down

1 comment on commit dbb2dc7

@awaitlink
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failure is because of a clippy warning which is a false positive (rust-lang/rust-clippy#11199):

warning: this argument is a mutable reference, but not used mutably
  --> src/network.rs:46:64
   |
46 | pub async fn json_from_response<T: DeserializeOwned>(response: &mut Response) -> anyhow::Result<T> {
   |                                                                ^^^^^^^^^^^^^ help: consider changing to: `&Response`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
   = note: `#[warn(clippy::needless_pass_by_ref_mut)]` on by default

warning: `signalupdates-bot` (lib) generated 1 warning

Applying it causes an error which is itself wrong (rust-lang/rust#113767):

error[E0596]: cannot borrow `*response` as mutable, as it is behind a `&` reference
  --> src/network.rs:47:5
   |
47 |     response
   |     ^^^^^^^^ `response` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |
help: consider specifying this binding's type
   |
46 | pub async fn json_from_response<T: DeserializeOwned>(response: &mut worker::Response: &Response) -> anyhow::Result<T> {
   |                                                              +++++++++++++++++++++++

Please sign in to comment.