You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/04/22 09:36:56 UTC
[incubator-opendal] branch refactor-request created (now b3139404)
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a change to branch refactor-request
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
at b3139404 refactor(raw/http_util): Add url in error context
This branch includes the following new commits:
new b3139404 refactor(raw/http_util): Add url in error context
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[incubator-opendal] 01/01: refactor(raw/http_util): Add url in error context
Posted by xu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch refactor-request
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit b31394045582e0fcbed06c7402aaf618e15ac4d9
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Sat Apr 22 17:36:16 2023 +0800
refactor(raw/http_util): Add url in error context
Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
core/src/raw/http_util/client.rs | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/core/src/raw/http_util/client.rs b/core/src/raw/http_util/client.rs
index bd4722ff..d90449d5 100644
--- a/core/src/raw/http_util/client.rs
+++ b/core/src/raw/http_util/client.rs
@@ -17,6 +17,7 @@
use std::fmt::Debug;
use std::fmt::Formatter;
+use std::mem;
use std::str::FromStr;
use futures::TryStreamExt;
@@ -79,14 +80,16 @@ impl HttpClient {
/// Send a request in async way.
pub async fn send(&self, req: Request<AsyncBody>) -> Result<Response<IncomingAsyncBody>> {
+ let url = req.uri().to_string();
let is_head = req.method() == http::Method::HEAD;
+
let (parts, body) = req.into_parts();
let mut req_builder = self
.client
.request(
parts.method,
- Url::from_str(&parts.uri.to_string()).expect("input request url must be valid"),
+ Url::from_str(&url).expect("input request url must be valid"),
)
.version(parts.version)
.headers(parts.headers);
@@ -101,7 +104,7 @@ impl HttpClient {
req_builder.body(body)
};
- let resp = req_builder.send().await.map_err(|err| {
+ let mut resp = req_builder.send().await.map_err(|err| {
let is_temporary = !(
// Builder related error should not be retried.
err.is_builder() ||
@@ -117,6 +120,7 @@ impl HttpClient {
let mut oerr = Error::new(ErrorKind::Unexpected, "send async request")
.with_operation("http_util::Client::send_async")
+ .with_context("url", &url)
.set_source(err);
if is_temporary {
oerr = oerr.set_temporary();
@@ -136,15 +140,15 @@ impl HttpClient {
let mut hr = Response::builder()
.version(resp.version())
.status(resp.status());
- for (k, v) in resp.headers().iter() {
- hr = hr.header(k, v);
- }
+ // Swap headers directly instead of copy the entire map.
+ mem::swap(hr.headers_mut().unwrap(), resp.headers_mut());
- let stream = resp.bytes_stream().map_err(|err| {
+ let stream = resp.bytes_stream().map_err(move |err| {
// If stream returns a body related error, we can convert
// it to interrupt so we can retry it.
Error::new(ErrorKind::Unexpected, "read data from http stream")
.map(|v| if err.is_body() { v.set_temporary() } else { v })
+ .with_context("url", &url)
.set_source(err)
});