we have a problem with calling our batchrequest API. In our backend, batchrequest API is…
- User
we have a problem with calling our batch_request API.
In our backend, batch_request API is a special API where the backend basically executes multiple API requests in series within an atomic transaction block. so if any of the request is not successful, all the changes made by any other requests within the batch are rolled back. i.e., if any of the sub-requests fails, the entire batch fails (returning a 4xx).
however, the errors of each response is within the sub-response of each request's response. hence our global error handler fails here.
because let's say if first error is success, second failed, and third success, the global error handler will toast something went wrong for the success ones, (because the whole batch had a failure status code, but it's some of it's sub-response din't have an error and hence goes with the fallback).
the solution i was thinking of was, we could create a
mutate.batchor updateuseBatchRequestwhich basically where we can pass array of batch request objects, it'd call the originalmutatefn itself however wraps it in a try catch to catch our HttpErrors only (else re-throw), and call the global httpErrorHandler by ourself for each of the non-successful responses only by iterating through those. this way we wouldn't need to change anything in the global error handler as well, nor write custom onErrors to handle these. (this would be similar to how we wrote query.paginated for paginating through all pages while querying a paginated API, or we could go with editing the behaviour of useBatchRequest itself directly to achieve the same).the code would be something like:
codeconst { mutate: batchRequest, isPending } = useBatchRequest(); // ... const submit = () => { const requests = []; // ... batchRequest(requests); }underneath something like this would happen:
codeconst mutation = useMutation({ mutationFn: (requests) => { try { // mutate(...) } catch (error) { if (!(error instanceof HTTPError && isBatchResult(error.cause))) { throw error; } const errorSubResponses = error.cause.results.filter((r) => r.status_code >= 400) // iterate errorSubResponses } } })ensure the reqeust/README.md is also updated (don't touch existing stuff, only about useBatchRequest).
All documentations/texts generated in this session should STRICTLY follow ASD-STE100 simplified technical english.
Assistant· claude-fable-5
Let me explore the existing request infrastructure.
Assistant· claude-fable-5
Now let me check the current consumers of
useBatchRequestto see how they handle errors.Assistant· claude-fable-5
The backend returns 400 with
{ results: [...] }where each failed sub-result hasstatus_code > 299and its error body indata. Let me check remaining consumers' error handling quickly.Assistant· claude-fable-5
I have full picture now. One design decision to confirm before writing the plan:
Assistant· claude-fable-5
Plan approved. Waiting for your go-ahead to implement.
- User
implement the plan
Assistant
Implementing the batch hook changes:
Assistant
Now verify
HTTPError.silentis mutable (not readonly) — confirmed in types.ts it's a plain public field. Now the README:Assistant
Now verify with tsc and lint on changed files:
Assistant
Dependencies aren't installed. Let me install them so I can type-check.
Assistant
The type predicate fails because
HTTPErrorCauselacks an index signature. Widening the guard's parameter tounknownfixes it. The@/supportedBrowserserror is pre-existing (generated by postinstall).Assistant
No type errors in the codebase. Now lint the changed file:
Assistant
Just an import ordering issue. Fixing:
Assistant
Clean. Let me review the final file and update todos.
Assistant
All checks pass. Marking complete:
Assistant
Done. Here's what I implemented:
src/Utils/request/batch.tsUpdated
useBatchRequestin place — no call-site changes needed, so all 4 existing consumers are fixed automatically:isBatchResponseguard — detects anHTTPErrorwhosecauseis a batch response (resultsarray of{ reference_id, status_code }).handleBatchSubErrors— for each failed sub-result (status_code > 299), builds a syntheticHTTPError(status + subdataas cause) and dispatches it to the existing globalhandleHttpError. So each failed sub-response gets full handling (pydantic, structured, 404, session expiry); successful sub-responses toast nothing.