From 6684a93b53ec5454eece06a9935260288fda60cb Mon Sep 17 00:00:00 2001 From: nodkz Date: Wed, 23 May 2018 17:27:53 +0600 Subject: [PATCH] fix(BatchMiddleware): add additional check for global errors --- src/middleware/__tests__/batch.test.js | 2 ++ src/middleware/batch.js | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/middleware/__tests__/batch.test.js b/src/middleware/__tests__/batch.test.js index 2e0f1d5..9ffa558 100644 --- a/src/middleware/__tests__/batch.test.js +++ b/src/middleware/__tests__/batch.test.js @@ -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); diff --git a/src/middleware/batch.js b/src/middleware/batch.js index 39c06c1..47c5cdd 100644 --- a/src/middleware/batch.js +++ b/src/middleware/batch.js @@ -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; @@ -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": {} }` + ) ); } });