Skip to content

fix: return 400 when code bus JSON has no :names #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/utils/json-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
29 changes: 28 additions & 1 deletion test/json-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.');
});
Expand Down
Loading