aboutsummaryrefslogtreecommitdiff
path: root/dbparse.js
diff options
context:
space:
mode:
authorJoel Kronqvist <work.joelkronqvist@gmail.com>2022-10-18 21:41:31 +0300
committerJoel Kronqvist <work.joelkronqvist@gmail.com>2022-10-18 21:41:31 +0300
commitc28fe49a7a303ef0440ccffd830e4949a1f2381f (patch)
tree802cf959381c9d4bcc5b1ce9fabd8d8a524ff834 /dbparse.js
parent82eb5ff8594db3b0382be812253f4dae3f06425f (diff)
downloadLYLLRuoka-c28fe49a7a303ef0440ccffd830e4949a1f2381f.tar.gz
LYLLRuoka-c28fe49a7a303ef0440ccffd830e4949a1f2381f.zip
Enhanced robustness in retrieving the example input and in the init (at reboot) script.
The server errored out if the database was empty and there came a request, as it didn't check for empty arrays in dbparse.js in the function getRandomIndex. Fixed by adding handling & recursion limit. Added in init.sh a check to see if logging in to MySQL is possible to prevent errors at startup.
Diffstat (limited to 'dbparse.js')
-rw-r--r--dbparse.js10
1 files changed, 7 insertions, 3 deletions
diff --git a/dbparse.js b/dbparse.js
index 29b9cb7..eddfdc8 100644
--- a/dbparse.js
+++ b/dbparse.js
@@ -173,10 +173,14 @@ function randInt(start, stop)
return start + Math.floor(Math.random() * (stop - start));
}
-async function getRandomIndex(day, DB)
+async function getRandomIndex(day, DB, depth=0)
{
+ if (depth > 10)
+ return null;
+
let indexes = await DB.execute("SELECT course, teacher, class FROM shifts WHERE day = ? ORDER BY RAND() LIMIT 1", [day]);
- indexes = Object.values(indexes[0]);
+
+ indexes = Object.values(indexes[0] || [null, null, null]); /// ERROR HERE!!!
let start = randInt(0, indexes.length);
for (let test = 0; test < 3; test++)
{
@@ -185,7 +189,7 @@ async function getRandomIndex(day, DB)
return indexes[i];
}
console.log("Warning: row without class/teacher/course in database!");
- return getRandomIndex(day, DB);
+ return getRandomIndex(day, DB, depth + 1);
}