Skip to content

Commit

Permalink
Merge pull request #67 from Shudrum/aggregate-allowed-status-codes
Browse files Browse the repository at this point in the history
Aggregate allow some responses status codes instead of failing
  • Loading branch information
maximebiloe authored Mar 6, 2020
2 parents a8f8678 + 8311665 commit 0c83b1c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/collections/_workers/aggregate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions test/workers/aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
17 changes: 14 additions & 3 deletions workers/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
};
Expand Down

0 comments on commit 0c83b1c

Please sign in to comment.