Skip to content

Commit

Permalink
Improves the messaging when configs fail validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoffy committed Jul 5, 2024
1 parent 0a00bd0 commit 6bf9234
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
11 changes: 10 additions & 1 deletion app/server/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ export class FileConfig<FileContents> {
rawFileContents = JSON.parse(await Deps.readFile(configPath, 'utf8'));
}

const fileContents = validator(rawFileContents);
let fileContents = null;

try {
fileContents = validator(rawFileContents);
} catch (error) {
const configError =
new ConfigValidationError(`Config at ${configPath} failed validation: ${error.message}`);
configError.cause = error;
throw configError;
}

if (!fileContents) {
throw new ConfigValidationError(`Config at ${configPath} failed validation - check the format?`);
Expand Down
7 changes: 3 additions & 4 deletions app/server/lib/configCoreFileFormats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ export function convertToCoreFileContents(input: any): IGristCoreConfigFileLates
configObject = upgradeV0toV1(configObject);
}

if (checkers.IGristCoreConfigFileLatest.test(configObject)) {
return configObject;
}
// This will throw an exception if the config object is still not in the correct format.
checkers.IGristCoreConfigFileLatest.check(configObject);

return null;
return configObject;
}
2 changes: 1 addition & 1 deletion test/server/lib/configCoreFileFormats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('convertToCoreFileContents', () => {
version: "This is a random version number that will never exist",
};

assert.isNull(convertToCoreFileContents(badConfig));
assert.throws(() => convertToCoreFileContents(badConfig));
});

// This is necessary to handle users who don't have a config file yet.
Expand Down

0 comments on commit 6bf9234

Please sign in to comment.