diff --git a/package.json b/package.json index a6b2f336..3c125d84 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,7 @@ ], "recursive": "true", "reporter": "mocha-multi-reporters", - "reporter-options": "configFile=.mocha-multi.json", - "loader": "esmock" + "reporter-options": "configFile=.mocha-multi.json" }, "engines": { "node": ">=16.x" diff --git a/src/utils/json-filter.js b/src/utils/json-filter.js index f07ceb48..e7c082d8 100644 --- a/src/utils/json-filter.js +++ b/src/utils/json-filter.js @@ -44,7 +44,7 @@ export default function jsonFilter(state, res, query) { }; } - const { data } = state.content; + const { data, sourceBus } = state.content; let json; try { state.timer?.update('json-parse'); @@ -75,7 +75,7 @@ export default function jsonFilter(state, res, query) { } if (!json[NAMES_KEY]) { - throw new PipelineStatusError(502, 'multisheet data invalid. missing ":names" property.'); + throw new PipelineStatusError(sourceBus === 'code' ? 400 : 502, 'multisheet data invalid. missing ":names" property.'); } state.timer?.update('json-filter'); diff --git a/test/json-pipe.test.js b/test/json-pipe.test.js index 8de1e062..99a9664b 100644 --- a/test/json-pipe.test.js +++ b/test/json-pipe.test.js @@ -519,7 +519,7 @@ describe('JSON Pipe Test', () => { assert.strictEqual(resp.status, 500); }); - it('handles error from filter', async () => { + it('handles error from filter (code)', async () => { const state = DEFAULT_STATE({ path: '/en/index.json', config: DEFAULT_CONFIG, @@ -542,6 +542,33 @@ describe('JSON Pipe Test', () => { ), }); const resp = await jsonPipe(state, new PipelineRequest('https://json-filter.com/?limit=5')); + assert.strictEqual(resp.status, 400); + assert.strictEqual(resp.headers.get('x-error'), 'multisheet data invalid. missing ":names" property.'); + }); + + it('handles error from filter (content)', async () => { + const state = DEFAULT_STATE({ + path: '/en/index.json', + config: DEFAULT_CONFIG, + ref: 'ref', + partition: 'preview', + s3Loader: new StaticS3Loader() + .reply( + 'helix-content-bus', + 'foobar/preview/en/index.json', + new PipelineResponse(JSON.stringify({ + version: 123, + message: 'hello, world', + }), { + headers: { + 'content-type': 'application/json', + 'x-amz-meta-x-source-location': 'foo-bar', + 'last-modified': 'Wed, 12 Oct 2009 17:50:00 GMT', + }, + }), + ), + }); + const resp = await jsonPipe(state, new PipelineRequest('https://json-filter.com/?limit=5')); assert.strictEqual(resp.status, 502); assert.strictEqual(resp.headers.get('x-error'), 'multisheet data invalid. missing ":names" property.'); });