From a4d20bc788209b062a4ea302d11b2b4387055631 Mon Sep 17 00:00:00 2001 From: ayou <550244300@qq.com> Date: Thu, 9 Oct 2025 23:31:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(http=E5=AE=A2=E6=88=B7=E7=AB=AF):=20?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E8=B6=85=E6=97=B6=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=B9=B6=E4=BF=9D=E7=95=99URL=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在请求发送和响应体读取时添加明确的超时错误处理,使用clone保留URL以便在错误信息中显示 --- backend/src/middlewares/http_client.rs | 33 +++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/backend/src/middlewares/http_client.rs b/backend/src/middlewares/http_client.rs index d84e1ad..30484e2 100644 --- a/backend/src/middlewares/http_client.rs +++ b/backend/src/middlewares/http_client.rs @@ -49,7 +49,8 @@ pub async fn execute_http(req: HttpRequest, opts: HttpClientOptions) -> Result Result r, + Err(e) => { + if e.is_timeout() { + let hint = match opts.timeout_ms { + Some(ms) => format!("http timeout: request {} timed out after {}ms", req.url, ms), + None => format!("http timeout: request {} timed out", req.url), + }; + return Err(anyhow::Error::new(e).context(hint)); + } + return Err(e.into()); + } + }; let status = resp.status().as_u16(); let headers_out: Map = resp .headers() @@ -93,7 +107,20 @@ pub async fn execute_http(req: HttpRequest, opts: HttpClientOptions) -> Result t, + Err(e) => { + if e.is_timeout() { + let hint = match opts.timeout_ms { + Some(ms) => format!("http timeout: reading body from {} timed out after {}ms", req.url, ms), + None => format!("http timeout: reading body from {} timed out", req.url), + }; + return Err(anyhow::Error::new(e).context(hint)); + } + return Err(e.into()); + } + }; let parsed_body: Value = serde_json::from_str(&text).unwrap_or_else(|_| Value::String(text)); Ok(HttpResponse {