Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw user-friendly error when config file fails to parse as JSON #961

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
);
}
});
2 changes: 1 addition & 1 deletion node-src/lib/getConfiguration.ts
Original file line number Diff line number Diff line change
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