From 8cdb9d6b8590eb5565b858e10d6ea37e1648b737 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Mon, 7 Oct 2024 12:03:02 -0700 Subject: [PATCH] Fix a bug where HTTP errors where being reported incorrectly (#18828) Release Notes: - N/A --------- Co-authored-by: Marshall Bowers --- crates/gpui/src/app.rs | 4 +++ crates/http_client/src/http_client.rs | 27 +++++++++++++++++++++ crates/reqwest_client/src/reqwest_client.rs | 6 ++++- crates/ureq_client/src/ureq_client.rs | 15 +++++++++++- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index bba5f857b4..f81a2092d5 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -1533,4 +1533,8 @@ impl HttpClient for NullHttpClient { fn proxy(&self) -> Option<&http_client::Uri> { None } + + fn type_name(&self) -> &'static str { + type_name::() + } } diff --git a/crates/http_client/src/http_client.rs b/crates/http_client/src/http_client.rs index 015c73a448..bf1046d88e 100644 --- a/crates/http_client/src/http_client.rs +++ b/crates/http_client/src/http_client.rs @@ -11,6 +11,7 @@ use http::request::Builder; #[cfg(feature = "test-support")] use std::fmt; use std::{ + any::type_name, sync::{Arc, LazyLock, Mutex}, time::Duration, }; @@ -72,6 +73,8 @@ impl HttpRequestExt for http::request::Builder { } pub trait HttpClient: 'static + Send + Sync { + fn type_name(&self) -> &'static str; + fn send( &self, req: http::Request, @@ -154,6 +157,10 @@ impl HttpClient for HttpClientWithProxy { fn proxy(&self) -> Option<&Uri> { self.proxy.as_ref() } + + fn type_name(&self) -> &'static str { + self.client.type_name() + } } impl HttpClient for Arc { @@ -167,6 +174,10 @@ impl HttpClient for Arc { fn proxy(&self) -> Option<&Uri> { self.proxy.as_ref() } + + fn type_name(&self) -> &'static str { + self.client.type_name() + } } /// An [`HttpClient`] that has a base URL. @@ -278,6 +289,10 @@ impl HttpClient for Arc { fn proxy(&self) -> Option<&Uri> { self.client.proxy.as_ref() } + + fn type_name(&self) -> &'static str { + self.client.type_name() + } } impl HttpClient for HttpClientWithUrl { @@ -291,6 +306,10 @@ impl HttpClient for HttpClientWithUrl { fn proxy(&self) -> Option<&Uri> { self.client.proxy.as_ref() } + + fn type_name(&self) -> &'static str { + self.client.type_name() + } } pub fn read_proxy_from_env() -> Option { @@ -331,6 +350,10 @@ impl HttpClient for BlockedHttpClient { fn proxy(&self) -> Option<&Uri> { None } + + fn type_name(&self) -> &'static str { + type_name::() + } } #[cfg(feature = "test-support")] @@ -403,4 +426,8 @@ impl HttpClient for FakeHttpClient { fn proxy(&self) -> Option<&Uri> { None } + + fn type_name(&self) -> &'static str { + type_name::() + } } diff --git a/crates/reqwest_client/src/reqwest_client.rs b/crates/reqwest_client/src/reqwest_client.rs index 6e84c58954..b5c274d599 100644 --- a/crates/reqwest_client/src/reqwest_client.rs +++ b/crates/reqwest_client/src/reqwest_client.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, io::Read, pin::Pin, task::Poll}; +use std::{any::type_name, borrow::Cow, io::Read, pin::Pin, task::Poll}; use anyhow::anyhow; use bytes::{BufMut, Bytes, BytesMut}; @@ -183,6 +183,10 @@ impl http_client::HttpClient for ReqwestClient { None } + fn type_name(&self) -> &'static str { + type_name::() + } + fn send( &self, req: http::Request, diff --git a/crates/ureq_client/src/ureq_client.rs b/crates/ureq_client/src/ureq_client.rs index 8951e80ac2..d3d8d3aedc 100644 --- a/crates/ureq_client/src/ureq_client.rs +++ b/crates/ureq_client/src/ureq_client.rs @@ -1,3 +1,4 @@ +use std::any::type_name; use std::collections::HashMap; use std::io::Read; use std::sync::Arc; @@ -74,6 +75,10 @@ impl HttpClient for UreqClient { self.proxy_url.as_ref() } + fn type_name(&self) -> &'static str { + type_name::() + } + fn send( &self, request: http::Request, @@ -100,7 +105,15 @@ impl HttpClient for UreqClient { self.background_executor .spawn(async move { - let response = req.send(body)?; + let response = match req.send(body) { + Ok(response) => response, + Err(e) => match e { + ureq::Error::Status(_, response) => response, + ureq::Error::Transport(transport) => { + anyhow::bail!(transport) + } + }, + }; let mut builder = http::Response::builder() .status(response.status())