diff --git a/packages/basics/src/url-connect.js b/packages/basics/src/url-connect.js index 089cec8b..c82fd613 100644 --- a/packages/basics/src/url-connect.js +++ b/packages/basics/src/url-connect.js @@ -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); diff --git a/packages/basics/test/empty.ini b/packages/basics/test/empty.ini new file mode 100644 index 00000000..ea51f09c --- /dev/null +++ b/packages/basics/test/empty.ini @@ -0,0 +1,2 @@ +[exchange] +value = fix('') diff --git a/packages/basics/test/url-connect.js b/packages/basics/test/url-connect.js index 6368bf17..1b8e7d9d 100644 --- a/packages/basics/test/url-connect.js +++ b/packages/basics/test/url-connect.js @@ -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); @@ -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);