Skip to content

Commit

Permalink
fix(BatchMiddleware): add additional check for global errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed May 23, 2018
1 parent 5a6592c commit 6684a93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/middleware/__tests__/batch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ describe('batchMiddleware', () => {
},
method: 'POST',
});

const req1 = mockReq(1);
const req2 = mockReq(2);

await rnl.sendQueries([req1, req2]).catch(() => {});

expect(req1.error).toBeInstanceOf(Error);
Expand Down
21 changes: 13 additions & 8 deletions src/middleware/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ function prepareNewBatcher(next, opts): Batcher {

setTimeout(() => {
batcher.acceptRequests = false;
sendRequests(batcher.requestMap, next, opts)
.then(() => finalizeUncompleted(batcher.requestMap))
.catch(() => finalizeUncompleted(batcher.requestMap));
try {
sendRequests(batcher.requestMap, next, opts)
.then(() => finalizeUncompleted(batcher.requestMap))
.catch(() => finalizeUncompleted(batcher.requestMap));
} catch (e) {
finalizeUncompleted(batcher.requestMap, e);
}
}, opts.batchTimeout);

return batcher;
Expand Down Expand Up @@ -201,15 +205,16 @@ function sendRequests(requestMap: BatchRequestMap, next, opts) {
}

// check that server returns responses for all requests
function finalizeUncompleted(requestMap: BatchRequestMap) {
function finalizeUncompleted(requestMap: BatchRequestMap, e?: Error) {
Object.keys(requestMap).forEach(id => {
const request = requestMap[id];
if (!request.done) {
request.completeErr(
new Error(
`Server does not return response for request with id ${id} \n` +
`Response should have following shape { "id": "${id}", "data": {} }`
)
e ||
new Error(
`Server does not return response for request with id ${id} \n` +
`Response should have following shape { "id": "${id}", "data": {} }`
)
);
}
});
Expand Down

0 comments on commit 6684a93

Please sign in to comment.