From f049f69757c09d5ff509793506606ec9f00c8109 Mon Sep 17 00:00:00 2001 From: Jacob <3012099+jshado1@users.noreply.github.com> Date: Tue, 19 Mar 2019 09:10:17 +0000 Subject: [PATCH 1/2] Remove try/catch throw to avoid swallowing JSON5 error output Resolves #33 --- src/index.js | 8 +------- test/json5-loader.test.js | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index d654d95..c599347 100644 --- a/src/index.js +++ b/src/index.js @@ -7,13 +7,7 @@ import util from 'util'; import JSON5 from 'json5'; function Json5Loader(source) { - let value; - - try { - value = JSON5.parse(source); - } catch (e) { - throw new Error('Error parsing JSON5', (e)); - } + const value = JSON5.parse(source); return `module.exports = ${util.inspect(value, { depth: null })}`; } diff --git a/test/json5-loader.test.js b/test/json5-loader.test.js index e1199fb..3b12cd6 100644 --- a/test/json5-loader.test.js +++ b/test/json5-loader.test.js @@ -19,7 +19,7 @@ describe(PROJECT_NAME, () => { const brokenJson5 = '{broken: json5}'; expect(() => { Json5Loader.call({}, brokenJson5); - }).toThrow('Error parsing JSON5'); + }).toThrow(SyntaxError); done(); }); From 293cbedc5d52f24c74909f3d245bc0fd50a06099 Mon Sep 17 00:00:00 2001 From: Jacob <3012099+jshado1@users.noreply.github.com> Date: Wed, 20 Mar 2019 09:04:38 +0000 Subject: [PATCH 2/2] Switch to emitError --- src/index.js | 8 +++++++- test/json5-loader.test.js | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index c599347..ebbae93 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,13 @@ import util from 'util'; import JSON5 from 'json5'; function Json5Loader(source) { - const value = JSON5.parse(source); + let value; + + try { + value = JSON5.parse(source); + } catch (e) { + this.emitError(e); + } return `module.exports = ${util.inspect(value, { depth: null })}`; } diff --git a/test/json5-loader.test.js b/test/json5-loader.test.js index 3b12cd6..3bb5fd6 100644 --- a/test/json5-loader.test.js +++ b/test/json5-loader.test.js @@ -15,11 +15,14 @@ describe(PROJECT_NAME, () => { done(); }); - test('should catch invalid JSON5', (done) => { + test('should handle invalid JSON5', (done) => { const brokenJson5 = '{broken: json5}'; - expect(() => { - Json5Loader.call({}, brokenJson5); - }).toThrow(SyntaxError); + const emitError = jest.fn(); + + Json5Loader.call({ + emitError, + }, brokenJson5); + expect(emitError).toHaveBeenCalledWith(expect.any(SyntaxError)); done(); });