Skip to content

Commit

Permalink
Stop overwriting response object (#3)
Browse files Browse the repository at this point in the history
* Stop overwriting the response object entirely

This change allows other onError middlewares to set response headers

* 0.1.2

Co-authored-by: Wojciech Iskra <[email protected]>
  • Loading branch information
domeq and domeq authored Oct 28, 2020
1 parent 9df8e5c commit f1f1f1e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = ({ logger = console, level = 'error' } = {}) => {

// eslint-disable-next-line no-param-reassign
handler.response = {
...R.propOr({}, 'response', handler),
statusCode: R.propOr(500, 'statusCode', error),
body: JSON.stringify(
R.filter(Boolean, {
Expand Down
31 changes: 31 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,34 @@ test('Middleware returns error details', async () => {

expect(mockLogger.error).toHaveBeenCalledTimes(1);
});

test('Keep data already present in response', async () => {
const handler = middy(async () => {
throw error;
});

// eslint-disable-next-line no-shadow
handler.onError(async (handler) => {
// eslint-disable-next-line no-param-reassign
handler.response = {
headers: {
someHeader: 'someValue',
},
};

return true;
});

handler.use(middleware({ logger: false }));

await expect(handler({}, {})).rejects.toEqual(
expect.objectContaining({
response: {
body: JSON.stringify({ statusCode: 404, message: 'File not found' }),
statusCode: 404,
headers: { someHeader: 'someValue' },
},
error,
})
);
});
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@schibsted/middy-error-handler",
"version": "0.1.1",
"version": "0.1.2",
"description": "Middy middleware for adding caching headers to success response and errors",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f1f1f1e

Please sign in to comment.