diff --git a/docs/collections/_workers/aggregate.md b/docs/collections/_workers/aggregate.md index 1424e36..48edbf6 100644 --- a/docs/collections/_workers/aggregate.md +++ b/docs/collections/_workers/aggregate.md @@ -23,6 +23,8 @@ body will be overwrited by the reponse ones. The third argument `options` is an object accepting these keys: + - `failStatusCodes`: Array of generic status codes which will break the workflow, starting the + worklows `onError` option. The default value is `[400, 500]`. - `path`: [object path](https://github.com/mariocasciaro/object-path){:target="_blank"} like destination for the response of the request. Like the `container`'s body, existing keys will be overwritted. diff --git a/package.json b/package.json index 7cf9047..801becc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nodegate", "description": "API gateway made simple, fast and easy to configure.", - "version": "1.3.1", + "version": "1.4.0", "author": "Julien Martin ", "license": "MIT", "scripts": { diff --git a/test/workers/aggregate.test.js b/test/workers/aggregate.test.js index ab10e15..f5db0af 100644 --- a/test/workers/aggregate.test.js +++ b/test/workers/aggregate.test.js @@ -169,5 +169,17 @@ describe('workers/aggregate', () => { expect(err.container.statusCode).toEqual(404); } }); + it('should not throw if the statusCode is not on the "failStatusCodes" option', async () => { + const container = getEmpty(); + nock('https://wiki.federation.com') + .post('/armaments') + .reply(404, { content: 'This article does not exists' }); + await aggregate( + 'post', + 'https://wiki.federation.com/armaments', + { failStatusCodes: [500] }, + )(container); + expect(container.body.content).toEqual('This article does not exists'); + }); }); }); diff --git a/workers/aggregate.js b/workers/aggregate.js index 40f7d53..5386033 100644 --- a/workers/aggregate.js +++ b/workers/aggregate.js @@ -27,6 +27,7 @@ const setBodyToContainer = (body, container, options) => { module.exports = (method, url, options = {}) => { const buildedUrl = urlBuilder(url); + const failStatusCodes = options.failStatusCodes || [400, 500]; return async (container) => { try { const { body, statusCode } = await request( @@ -38,12 +39,22 @@ module.exports = (method, url, options = {}) => { container.statusCode = statusCode; setBodyToContainer(body, container, options); } catch (err) { + const body = err.response && err.response.body; + const statusCode = err.response ? err.response.statusCode : 500; + + if (body && !failStatusCodes.includes(parseInt(`${`${statusCode}[0]`}00`, 10))) { + setBodyToContainer(body, container, options); + container.statusCode = statusCode; + return; + } + const error = new WorkflowError(err, err.response); error.setContainer(container); - if (err.response && err.response.body) { - container.errorBody = err.response.body; + if (body) { + container.errorBody = body; } - container.statusCode = err.response ? err.response.statusCode : 500; + container.statusCode = statusCode; + throw error; } };