Skip to content

Commit f3b103a

Browse files
authored
fix(reqwest-send): always do error_for_status()? for send() (#1047)
1 parent 5e09554 commit f3b103a

File tree

4 files changed

+24
-46
lines changed

4 files changed

+24
-46
lines changed

src/llm/anthropic.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,21 @@ impl LlmGenerationClient for Client {
8989
let encoded_api_key = encode(&self.api_key);
9090

9191
let resp = retryable::run(
92-
|| {
92+
|| async {
9393
self.client
9494
.post(url)
9595
.header("x-api-key", encoded_api_key.as_ref())
9696
.header("anthropic-version", "2023-06-01")
9797
.json(&payload)
9898
.send()
99+
.await?
100+
.error_for_status()
99101
},
100102
&retryable::HEAVY_LOADED_OPTIONS,
101103
)
102-
.await?;
103-
if !resp.status().is_success() {
104-
bail!(
105-
"Anthropic API error: {:?}\n{}\n",
106-
resp.status(),
107-
resp.text().await?
108-
);
109-
}
104+
.await
105+
.context("Anthropic API error")?;
106+
110107
let mut resp_json: serde_json::Value = resp.json().await.context("Invalid JSON")?;
111108
if let Some(error) = resp_json.get("error") {
112109
bail!("Anthropic API error: {:?}", error);

src/llm/ollama.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,18 @@ impl LlmGenerationClient for Client {
102102
stream: Some(false),
103103
};
104104
let res = retryable::run(
105-
|| {
105+
|| async {
106106
self.reqwest_client
107107
.post(self.generate_url.as_str())
108108
.json(&req)
109109
.send()
110+
.await?
111+
.error_for_status()
110112
},
111113
&retryable::HEAVY_LOADED_OPTIONS,
112114
)
113-
.await?;
114-
if !res.status().is_success() {
115-
bail!(
116-
"Ollama API error: {:?}\n{}\n",
117-
res.status(),
118-
res.text().await?
119-
);
120-
}
115+
.await
116+
.context("Ollama API error")?;
121117
let json: OllamaResponse = res.json().await?;
122118
Ok(super::LlmGenerateResponse {
123119
text: json.response,
@@ -145,22 +141,18 @@ impl LlmEmbeddingClient for Client {
145141
input: request.text.as_ref(),
146142
};
147143
let resp = retryable::run(
148-
|| {
144+
|| async {
149145
self.reqwest_client
150146
.post(self.embed_url.as_str())
151147
.json(&req)
152148
.send()
149+
.await?
150+
.error_for_status()
153151
},
154152
&retryable::HEAVY_LOADED_OPTIONS,
155153
)
156-
.await?;
157-
if !resp.status().is_success() {
158-
bail!(
159-
"Ollama API error: {:?}\n{}\n",
160-
resp.status(),
161-
resp.text().await?
162-
);
163-
}
154+
.await
155+
.context("Ollama API error")?;
164156
let embedding_resp: OllamaEmbeddingResponse = resp.json().await.context("Invalid JSON")?;
165157
Ok(super::LlmEmbeddingResponse {
166158
embedding: embedding_resp.embedding,

src/llm/voyage.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,19 @@ impl LlmEmbeddingClient for Client {
7676
}
7777

7878
let resp = retryable::run(
79-
|| {
79+
|| async {
8080
self.client
8181
.post(url)
8282
.header("Authorization", format!("Bearer {}", self.api_key))
8383
.json(&payload)
8484
.send()
85+
.await?
86+
.error_for_status()
8587
},
8688
&retryable::HEAVY_LOADED_OPTIONS,
8789
)
88-
.await?;
89-
90-
if !resp.status().is_success() {
91-
bail!(
92-
"Voyage AI API error: {:?}\n{}\n",
93-
resp.status(),
94-
resp.text().await?
95-
);
96-
}
90+
.await
91+
.context("Voyage AI API error")?;
9792

9893
let embedding_resp: EmbedResponse = resp.json().await.context("Invalid JSON")?;
9994

src/ops/targets/kuzu.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,12 @@ impl KuzuThinClient {
8080
let query = json!({
8181
"query": cyper_builder.query
8282
});
83-
let response = self
84-
.reqwest_client
83+
self.reqwest_client
8584
.post(&self.query_url)
8685
.json(&query)
8786
.send()
88-
.await?;
89-
if !response.status().is_success() {
90-
return Err(anyhow::anyhow!(
91-
"Failed to run cypher: {}",
92-
response.text().await?
93-
));
94-
}
87+
.await?
88+
.error_for_status()?;
9589
Ok(())
9690
}
9791
}

0 commit comments

Comments
 (0)