From 0430284a9231f621950683c1d5135f1f4dfa5f5c Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Wed, 30 Nov 2022 13:59:31 -0600 Subject: [PATCH 1/2] Log more robustly when JSON parsing fails --- packages/json/src/index.js | 3 +-- packages/json/test/test.js | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/json/src/index.js b/packages/json/src/index.js index 63e37c280..9dd8d81db 100755 --- a/packages/json/src/index.js +++ b/packages/json/src/index.js @@ -24,8 +24,7 @@ export default function json(options = {}) { }; } catch (err) { const message = 'Could not parse JSON file'; - const position = parseInt(/[\d]/.exec(err.message)[0], 10); - this.warn({ message, id, position }); + this.warn({ message, id, cause: err }); return null; } } diff --git a/packages/json/test/test.js b/packages/json/test/test.js index 95ce01d33..392f070da 100755 --- a/packages/json/test/test.js +++ b/packages/json/test/test.js @@ -81,11 +81,11 @@ test('handles garbage', async (t) => { onwarn: (warning) => warns.push(warning) }).catch(() => {}); - const [{ message, id, position, plugin }] = warns; + const [{ message, id, cause, plugin }] = warns; t.is(warns.length, 1); t.is(plugin, 'json'); - t.is(position, 1); + t.true(cause instanceof SyntaxError); t.is(message, 'Could not parse JSON file'); t.regex(id, /(.*)bad.json$/); }); From 2b865d8268ad41abda5960d7d05a1f1c9b0b0197 Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Mon, 5 Dec 2022 12:18:41 -0600 Subject: [PATCH 2/2] changed to error per pr feedback --- packages/json/src/index.js | 2 +- packages/json/test/test.js | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/json/src/index.js b/packages/json/src/index.js index 9dd8d81db..b61ed2300 100755 --- a/packages/json/src/index.js +++ b/packages/json/src/index.js @@ -24,7 +24,7 @@ export default function json(options = {}) { }; } catch (err) { const message = 'Could not parse JSON file'; - this.warn({ message, id, cause: err }); + this.error({ message, id, cause: err }); return null; } } diff --git a/packages/json/test/test.js b/packages/json/test/test.js index 392f070da..1abbc2377 100755 --- a/packages/json/test/test.js +++ b/packages/json/test/test.js @@ -73,21 +73,18 @@ test('handles JSON objects with no valid keys (#19)', async (t) => { }); test('handles garbage', async (t) => { - const warns = []; - - await rollup({ - input: 'fixtures/garbage/main.js', - plugins: [json()], - onwarn: (warning) => warns.push(warning) - }).catch(() => {}); - - const [{ message, id, cause, plugin }] = warns; - - t.is(warns.length, 1); - t.is(plugin, 'json'); - t.true(cause instanceof SyntaxError); - t.is(message, 'Could not parse JSON file'); - t.regex(id, /(.*)bad.json$/); + const err = await t.throwsAsync( + rollup({ + input: 'fixtures/garbage/main.js', + plugins: [json()] + }) + ); + t.is(err.code, 'PLUGIN_ERROR'); + t.is(err.plugin, 'json'); + t.is(err.message, 'Could not parse JSON file'); + t.is(err.name, 'RollupError'); + t.is(err.cause.name, 'SyntaxError'); + t.regex(err.id, /(.*)bad.json$/); }); test('does not generate an AST', async (t) => {