Skip to content

Commit

Permalink
Merge pull request #961 from chromaui/ghengeveld/247-configuration-fi…
Browse files Browse the repository at this point in the history
…le-json-parse-error

Throw user-friendly error when config file fails to parse as JSON
  • Loading branch information
ghengeveld committed Mar 29, 2024
2 parents d00be31 + d03f66d commit ed70811
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions node-src/lib/getConfiguration.test.ts
Expand Up @@ -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/
);
}
{
mockedReadFile.mockReturnValue('{ "foo": 1 "unexpectedString": 2 }');
await expect(getConfiguration('test.file')).rejects.toThrow(
/Configuration file .+ could not be parsed/
);
}
{
mockedReadFile.mockReturnValue('{ "unexpectedEnd": ');
await expect(getConfiguration('test.file')).rejects.toThrow(
/Configuration file .+ could not be parsed/
);
}
});
2 changes: 1 addition & 1 deletion node-src/lib/getConfiguration.ts
Expand Up @@ -62,7 +62,7 @@ export async function getConfiguration(
throw new Error(missingConfigurationFile(configFile));
}
}
if (err.message.match('Unexpected string')) {
if (err instanceof SyntaxError) {
throw new Error(unparseableConfigurationFile(usedConfigFile, err));
}
if (err instanceof ZodError) {
Expand Down

0 comments on commit ed70811

Please sign in to comment.