From 97020c382d21bd9f6a3158c0647143e43fce8b34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Wed, 9 Aug 2023 12:29:25 +0200
Subject: [PATCH] ExerciceHelper => Add some catch on JSON5 parsing

---
 helpers/ExerciceHelper.ts | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/helpers/ExerciceHelper.ts b/helpers/ExerciceHelper.ts
index a51f113..c6ff93b 100644
--- a/helpers/ExerciceHelper.ts
+++ b/helpers/ExerciceHelper.ts
@@ -6,7 +6,7 @@ import JSON5                               from 'json5';
 
 class ExerciceHelper {
     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();
 
@@ -34,14 +34,22 @@ class ExerciceHelper {
 
         const validator = ajv.compile(schema);
 
-        const results = JSON5.parse(fs.readFileSync(resultsFilePath, 'utf8'));
-        const isValid = validator(results);
-
-        return {
-            results: isValid ? results as ExerciceResultsFile : results as any,
-            isValid: isValid,
-            errors : validator.errors
-        };
+        try {
+            const results = JSON5.parse(fs.readFileSync(resultsFilePath, 'utf8'));
+            const isValid = validator(results);
+
+            return {
+                results: isValid ? results as ExerciceResultsFile : results as any,
+                isValid: isValid,
+                errors : validator.errors
+            };
+        } catch ( error ) {
+            return {
+                results: undefined,
+                isValid: false,
+                errors : [ `JSON5 invalid : ${ JSON.stringify(error) }` ]
+            };
+        }
     }
 }
 
-- 
GitLab