Skip to content

Commit bf71d9c

Browse files
committed
better error handling
1 parent 9e8d3a1 commit bf71d9c

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

clients/rust/src/many.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,10 @@ impl OperationResult {
191191
let mut headers = field.headers().clone();
192192
let key = headers
193193
.remove(HEADER_BATCH_OPERATION_KEY)
194-
.and_then(|v| v.to_str().ok().map(|s| s.to_owned()))
195-
.ok_or_else(|| {
196-
Error::MalformedResponse(format!(
197-
"missing or invalid {HEADER_BATCH_OPERATION_KEY} header"
198-
))
199-
})?;
194+
.and_then(|v| v.to_str().ok().map(|s| s.to_owned()));
200195
let kind = headers
201196
.remove(HEADER_BATCH_OPERATION_KIND)
202-
.and_then(|v| v.to_str().ok().map(|s| s.to_owned()))
203-
.ok_or_else(|| {
204-
Error::MalformedResponse(format!(
205-
"missing or invalid {HEADER_BATCH_OPERATION_KIND} header"
206-
))
207-
})?;
197+
.and_then(|v| v.to_str().ok().map(|s| s.to_owned()));
208198
let status: u16 = headers
209199
.remove(HEADER_BATCH_OPERATION_STATUS)
210200
.and_then(|v| v.to_str().ok().and_then(|s| s.parse().ok()))
@@ -214,6 +204,25 @@ impl OperationResult {
214204
))
215205
})?;
216206

207+
// If both key and kind are missing, this is a request-level error (e.g., rate limiting)
208+
// that applies to the entire batch rather than a specific operation.
209+
let (key, kind) = match (key, kind) {
210+
(Some(k), Some(kd)) => (k, kd),
211+
(None, None) => {
212+
// Request-level error: extract error details from body and status
213+
let body = field.bytes().await?;
214+
let message = String::from_utf8_lossy(&body).into_owned();
215+
return Err(Error::OperationError { status, message });
216+
}
217+
_ => {
218+
// One header present but not the other is malformed
219+
return Err(Error::MalformedResponse(format!(
220+
"inconsistent batch headers: both {HEADER_BATCH_OPERATION_KEY} and \
221+
{HEADER_BATCH_OPERATION_KIND} must be present or absent together"
222+
)));
223+
}
224+
};
225+
217226
let body = field.bytes().await?;
218227

219228
let is_error = status >= 400 && !(kind == "get" && status == 404);

0 commit comments

Comments
 (0)