Skip to content
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

fix: 🐛 explicit error message for empty response #421

Merged
merged 4 commits into from
Jul 9, 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
11 changes: 10 additions & 1 deletion packages/basics/src/url-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ export default async function URLConnect(data, feed) {
}
if (json) {
const bodyOutRaw = await getStream(response.body);
const bodyOutArray = JSON.parse(bodyOutRaw);
if (bodyOutRaw === '') {
throw new Error('URL returned an empty response');
}
let bodyOutArray;
try {
bodyOutArray = JSON.parse(bodyOutRaw);
}
catch (e) {
throw new Error(`URL returned an invalid JSON response (${bodyOutRaw})`);
}
return from(bodyOutArray).pipe(output);
}
return response.body.pipe(output);
Expand Down
2 changes: 2 additions & 0 deletions packages/basics/test/empty.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[exchange]
value = fix('')
29 changes: 27 additions & 2 deletions packages/basics/test/url-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ describe('URLConnect', () => {
.pipe(ezs.catch())
.on('error', (e) => {
try {
expect(e.message).toEqual(expect.stringContaining('JSON at position'));
expect(e.message).toEqual(expect.stringContaining('URL returned an invalid JSON response'));
done();
} catch(ee) {
done(ee);
Expand All @@ -232,7 +232,32 @@ describe('URLConnect', () => {
.pipe(ezs.catch())
.on('error', (e) => {
try {
expect(e.message).toEqual(expect.stringContaining("Invalid JSON (Unexpected \"\\r\" at position 3 in state STOP)"));
expect(e.message).toEqual(expect.stringContaining('Invalid JSON'));
done();
} catch(ee) {
done(ee);
}
})
.on('data', () => {
done(new Error('Error is the right behavior'));
})
.on('end', () => {
done(new Error('Error is the right behavior'));
});
});
test('#4ter', (done) => {
ezs.use(statements);
const input = ['1a', '2a', '3a', '4a', '5a'];
from(input)
.pipe(ezs('URLConnect', {
url: 'http://127.0.0.1:33331/empty.ini',
json: true,
retries: 1,
}))
.pipe(ezs.catch())
.on('error', (e) => {
try {
expect(e.message).toEqual(expect.stringContaining('URL returned an empty response'));
done();
} catch(ee) {
done(ee);
Expand Down
Loading