Skip to content

Commit

Permalink
Catch any SyntaxError
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Mar 28, 2024
1 parent 9da6494 commit 3a12043
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 21 additions & 0 deletions node-src/lib/getConfiguration.test.ts
Original file line number Diff line number Diff line change
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(.|\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
);
}
});
5 changes: 1 addition & 4 deletions node-src/lib/getConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.name === 'SyntaxError') {
throw new Error(unparseableConfigurationFile(usedConfigFile, err));
}
if (err instanceof ZodError) {
Expand Down

0 comments on commit 3a12043

Please sign in to comment.