Skip to content

Commit fed7cae

Browse files
fix: return 400 when code bus JSON has no :names (#724)
1 parent 5bf07a9 commit fed7cae

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
],
3737
"recursive": "true",
3838
"reporter": "mocha-multi-reporters",
39-
"reporter-options": "configFile=.mocha-multi.json",
40-
"loader": "esmock"
39+
"reporter-options": "configFile=.mocha-multi.json"
4140
},
4241
"engines": {
4342
"node": ">=16.x"

src/utils/json-filter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default function jsonFilter(state, res, query) {
4444
};
4545
}
4646

47-
const { data } = state.content;
47+
const { data, sourceBus } = state.content;
4848
let json;
4949
try {
5050
state.timer?.update('json-parse');
@@ -75,7 +75,7 @@ export default function jsonFilter(state, res, query) {
7575
}
7676

7777
if (!json[NAMES_KEY]) {
78-
throw new PipelineStatusError(502, 'multisheet data invalid. missing ":names" property.');
78+
throw new PipelineStatusError(sourceBus === 'code' ? 400 : 502, 'multisheet data invalid. missing ":names" property.');
7979
}
8080

8181
state.timer?.update('json-filter');

test/json-pipe.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ describe('JSON Pipe Test', () => {
519519
assert.strictEqual(resp.status, 500);
520520
});
521521

522-
it('handles error from filter', async () => {
522+
it('handles error from filter (code)', async () => {
523523
const state = DEFAULT_STATE({
524524
path: '/en/index.json',
525525
config: DEFAULT_CONFIG,
@@ -542,6 +542,33 @@ describe('JSON Pipe Test', () => {
542542
),
543543
});
544544
const resp = await jsonPipe(state, new PipelineRequest('https://json-filter.com/?limit=5'));
545+
assert.strictEqual(resp.status, 400);
546+
assert.strictEqual(resp.headers.get('x-error'), 'multisheet data invalid. missing ":names" property.');
547+
});
548+
549+
it('handles error from filter (content)', async () => {
550+
const state = DEFAULT_STATE({
551+
path: '/en/index.json',
552+
config: DEFAULT_CONFIG,
553+
ref: 'ref',
554+
partition: 'preview',
555+
s3Loader: new StaticS3Loader()
556+
.reply(
557+
'helix-content-bus',
558+
'foobar/preview/en/index.json',
559+
new PipelineResponse(JSON.stringify({
560+
version: 123,
561+
message: 'hello, world',
562+
}), {
563+
headers: {
564+
'content-type': 'application/json',
565+
'x-amz-meta-x-source-location': 'foo-bar',
566+
'last-modified': 'Wed, 12 Oct 2009 17:50:00 GMT',
567+
},
568+
}),
569+
),
570+
});
571+
const resp = await jsonPipe(state, new PipelineRequest('https://json-filter.com/?limit=5'));
545572
assert.strictEqual(resp.status, 502);
546573
assert.strictEqual(resp.headers.get('x-error'), 'multisheet data invalid. missing ":names" property.');
547574
});

0 commit comments

Comments
 (0)