Skip to content
Snippets Groups Projects
Commit 97020c38 authored by michael.minelli's avatar michael.minelli
Browse files

ExerciceHelper => Add some catch on JSON5 parsing

parent c6e9f764
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ import JSON5 from 'json5'; ...@@ -6,7 +6,7 @@ import JSON5 from 'json5';
class ExerciceHelper { class ExerciceHelper {
validateResultFile(resultsFilePath: string): { validateResultFile(resultsFilePath: string): {
results: ExerciceResultsFile, isValid: boolean, errors: ErrorObject<string, Record<string, any>, unknown>[] | null | undefined results: ExerciceResultsFile | undefined, isValid: boolean, errors: Array<ErrorObject<string, Record<string, any>, unknown> | string> | null | undefined
} { } {
const ajv = new Ajv(); const ajv = new Ajv();
...@@ -34,6 +34,7 @@ class ExerciceHelper { ...@@ -34,6 +34,7 @@ class ExerciceHelper {
const validator = ajv.compile(schema); const validator = ajv.compile(schema);
try {
const results = JSON5.parse(fs.readFileSync(resultsFilePath, 'utf8')); const results = JSON5.parse(fs.readFileSync(resultsFilePath, 'utf8'));
const isValid = validator(results); const isValid = validator(results);
...@@ -42,6 +43,13 @@ class ExerciceHelper { ...@@ -42,6 +43,13 @@ class ExerciceHelper {
isValid: isValid, isValid: isValid,
errors : validator.errors errors : validator.errors
}; };
} catch ( error ) {
return {
results: undefined,
isValid: false,
errors : [ `JSON5 invalid : ${ JSON.stringify(error) }` ]
};
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment