Skip to content

Commit

Permalink
Merge pull request #421 from Inist-CNRS/explicit-error-for-empty-resp…
Browse files Browse the repository at this point in the history
…onse

fix: 🐛 explicit error message for empty response
  • Loading branch information
touv authored Jul 9, 2024
2 parents 0cbe93a + 8c3fd81 commit 05d1b46
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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

0 comments on commit 05d1b46

Please sign in to comment.