From 9da6494ff48903333b0356424e0a138e37bd3fc5 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Thu, 28 Mar 2024 08:42:00 +0100 Subject: [PATCH 1/4] Throw user-friendly error when config file fails to parse as JSON --- node-src/lib/getConfiguration.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/node-src/lib/getConfiguration.ts b/node-src/lib/getConfiguration.ts index a47ea5955..7e5720a39 100644 --- a/node-src/lib/getConfiguration.ts +++ b/node-src/lib/getConfiguration.ts @@ -62,7 +62,10 @@ export async function getConfiguration( throw new Error(missingConfigurationFile(configFile)); } } - if (err.message.match('Unexpected string')) { + if ( + err.message.match('Unexpected string') || + err.message.match('Unexpected end of JSON input') + ) { throw new Error(unparseableConfigurationFile(usedConfigFile, err)); } if (err instanceof ZodError) { From 2a11ca3c8c719fc5a1ecc61e9c0206ae04029355 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Thu, 28 Mar 2024 11:16:35 +0100 Subject: [PATCH 2/4] Catch any SyntaxError --- node-src/lib/getConfiguration.test.ts | 21 +++++++++++++++++++++ node-src/lib/getConfiguration.ts | 5 +---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/node-src/lib/getConfiguration.test.ts b/node-src/lib/getConfiguration.test.ts index d90f9a92f..178c2a7a9 100644 --- a/node-src/lib/getConfiguration.test.ts +++ b/node-src/lib/getConfiguration.test.ts @@ -151,3 +151,24 @@ it('errors if config file contains unknown keys', async () => { await expect(getConfiguration('test.file')).rejects.toThrow(/random/); }); + +it('errors if config file is unparseable', async () => { + { + mockedReadFile.mockReturnValue('invalid json'); + await expect(getConfiguration('test.file')).rejects.toThrow( + /Configuration file .+ could not be parsed(.|\n)*Unexpected token i in JSON/ + ); + } + { + mockedReadFile.mockReturnValue('{ "foo": 1 "unexpectedString": 2 }'); + await expect(getConfiguration('test.file')).rejects.toThrow( + /Configuration file .+ could not be parsed(.|\n)*Unexpected string in JSON/m + ); + } + { + mockedReadFile.mockReturnValue('{ "unexpectedEnd": '); + await expect(getConfiguration('test.file')).rejects.toThrow( + /Configuration file .+ could not be parsed(.|\n)*Unexpected end of JSON input/m + ); + } +}); diff --git a/node-src/lib/getConfiguration.ts b/node-src/lib/getConfiguration.ts index 7e5720a39..2d5d22700 100644 --- a/node-src/lib/getConfiguration.ts +++ b/node-src/lib/getConfiguration.ts @@ -62,10 +62,7 @@ export async function getConfiguration( throw new Error(missingConfigurationFile(configFile)); } } - if ( - err.message.match('Unexpected string') || - err.message.match('Unexpected end of JSON input') - ) { + if (err instanceof SyntaxError) { throw new Error(unparseableConfigurationFile(usedConfigFile, err)); } if (err instanceof ZodError) { From 3cce85641ef967e25c5f19792f7a21168066de5a Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Thu, 28 Mar 2024 16:26:15 +0100 Subject: [PATCH 3/4] Simplify assertions to handle Node version difference --- node-src/lib/getConfiguration.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node-src/lib/getConfiguration.test.ts b/node-src/lib/getConfiguration.test.ts index 178c2a7a9..619c4d9fd 100644 --- a/node-src/lib/getConfiguration.test.ts +++ b/node-src/lib/getConfiguration.test.ts @@ -156,19 +156,19 @@ it('errors if config file is unparseable', async () => { { mockedReadFile.mockReturnValue('invalid json'); await expect(getConfiguration('test.file')).rejects.toThrow( - /Configuration file .+ could not be parsed(.|\n)*Unexpected token i in JSON/ + /Configuration file .+ could not be parsed(.|\n)*Unexpected token/ ); } { mockedReadFile.mockReturnValue('{ "foo": 1 "unexpectedString": 2 }'); await expect(getConfiguration('test.file')).rejects.toThrow( - /Configuration file .+ could not be parsed(.|\n)*Unexpected string in JSON/m + /Configuration file .+ could not be parsed(.|\n)*Unexpected string/m ); } { mockedReadFile.mockReturnValue('{ "unexpectedEnd": '); await expect(getConfiguration('test.file')).rejects.toThrow( - /Configuration file .+ could not be parsed(.|\n)*Unexpected end of JSON input/m + /Configuration file .+ could not be parsed(.|\n)*Unexpected end/m ); } }); From d03f66d51d9b71f02fae09bd6106a7bc29c6b44a Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Thu, 28 Mar 2024 16:38:45 +0100 Subject: [PATCH 4/4] Ignore error specifics --- node-src/lib/getConfiguration.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node-src/lib/getConfiguration.test.ts b/node-src/lib/getConfiguration.test.ts index 619c4d9fd..a80d38989 100644 --- a/node-src/lib/getConfiguration.test.ts +++ b/node-src/lib/getConfiguration.test.ts @@ -156,19 +156,19 @@ it('errors if config file is unparseable', async () => { { mockedReadFile.mockReturnValue('invalid json'); await expect(getConfiguration('test.file')).rejects.toThrow( - /Configuration file .+ could not be parsed(.|\n)*Unexpected token/ + /Configuration file .+ could not be parsed/ ); } { mockedReadFile.mockReturnValue('{ "foo": 1 "unexpectedString": 2 }'); await expect(getConfiguration('test.file')).rejects.toThrow( - /Configuration file .+ could not be parsed(.|\n)*Unexpected string/m + /Configuration file .+ could not be parsed/ ); } { mockedReadFile.mockReturnValue('{ "unexpectedEnd": '); await expect(getConfiguration('test.file')).rejects.toThrow( - /Configuration file .+ could not be parsed(.|\n)*Unexpected end/m + /Configuration file .+ could not be parsed/ ); } });