From 9877ecfe0e57e1826f68b86cedb924fa422a7acf Mon Sep 17 00:00:00 2001 From: Jiangzhou He Date: Wed, 24 Sep 2025 20:17:21 -0700 Subject: [PATCH] fix(reqwest-send): always do `error_for_status()?` for `send()` --- src/llm/anthropic.rs | 15 ++++++--------- src/llm/ollama.rs | 28 ++++++++++------------------ src/llm/voyage.rs | 15 +++++---------- src/ops/targets/kuzu.rs | 12 +++--------- 4 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/llm/anthropic.rs b/src/llm/anthropic.rs index bfb3c3117..d81f5d769 100644 --- a/src/llm/anthropic.rs +++ b/src/llm/anthropic.rs @@ -89,24 +89,21 @@ impl LlmGenerationClient for Client { let encoded_api_key = encode(&self.api_key); let resp = retryable::run( - || { + || async { self.client .post(url) .header("x-api-key", encoded_api_key.as_ref()) .header("anthropic-version", "2023-06-01") .json(&payload) .send() + .await? + .error_for_status() }, &retryable::HEAVY_LOADED_OPTIONS, ) - .await?; - if !resp.status().is_success() { - bail!( - "Anthropic API error: {:?}\n{}\n", - resp.status(), - resp.text().await? - ); - } + .await + .context("Anthropic API error")?; + let mut resp_json: serde_json::Value = resp.json().await.context("Invalid JSON")?; if let Some(error) = resp_json.get("error") { bail!("Anthropic API error: {:?}", error); diff --git a/src/llm/ollama.rs b/src/llm/ollama.rs index bc09c9386..b2f891c62 100644 --- a/src/llm/ollama.rs +++ b/src/llm/ollama.rs @@ -102,22 +102,18 @@ impl LlmGenerationClient for Client { stream: Some(false), }; let res = retryable::run( - || { + || async { self.reqwest_client .post(self.generate_url.as_str()) .json(&req) .send() + .await? + .error_for_status() }, &retryable::HEAVY_LOADED_OPTIONS, ) - .await?; - if !res.status().is_success() { - bail!( - "Ollama API error: {:?}\n{}\n", - res.status(), - res.text().await? - ); - } + .await + .context("Ollama API error")?; let json: OllamaResponse = res.json().await?; Ok(super::LlmGenerateResponse { text: json.response, @@ -145,22 +141,18 @@ impl LlmEmbeddingClient for Client { input: request.text.as_ref(), }; let resp = retryable::run( - || { + || async { self.reqwest_client .post(self.embed_url.as_str()) .json(&req) .send() + .await? + .error_for_status() }, &retryable::HEAVY_LOADED_OPTIONS, ) - .await?; - if !resp.status().is_success() { - bail!( - "Ollama API error: {:?}\n{}\n", - resp.status(), - resp.text().await? - ); - } + .await + .context("Ollama API error")?; let embedding_resp: OllamaEmbeddingResponse = resp.json().await.context("Invalid JSON")?; Ok(super::LlmEmbeddingResponse { embedding: embedding_resp.embedding, diff --git a/src/llm/voyage.rs b/src/llm/voyage.rs index 5e713cf3e..ea20e7d21 100644 --- a/src/llm/voyage.rs +++ b/src/llm/voyage.rs @@ -76,24 +76,19 @@ impl LlmEmbeddingClient for Client { } let resp = retryable::run( - || { + || async { self.client .post(url) .header("Authorization", format!("Bearer {}", self.api_key)) .json(&payload) .send() + .await? + .error_for_status() }, &retryable::HEAVY_LOADED_OPTIONS, ) - .await?; - - if !resp.status().is_success() { - bail!( - "Voyage AI API error: {:?}\n{}\n", - resp.status(), - resp.text().await? - ); - } + .await + .context("Voyage AI API error")?; let embedding_resp: EmbedResponse = resp.json().await.context("Invalid JSON")?; diff --git a/src/ops/targets/kuzu.rs b/src/ops/targets/kuzu.rs index 4e8bd106f..7cd9767eb 100644 --- a/src/ops/targets/kuzu.rs +++ b/src/ops/targets/kuzu.rs @@ -80,18 +80,12 @@ impl KuzuThinClient { let query = json!({ "query": cyper_builder.query }); - let response = self - .reqwest_client + self.reqwest_client .post(&self.query_url) .json(&query) .send() - .await?; - if !response.status().is_success() { - return Err(anyhow::anyhow!( - "Failed to run cypher: {}", - response.text().await? - )); - } + .await? + .error_for_status()?; Ok(()) } }