From 970e461621e6906ce7259b9dfcacbff90dae858b Mon Sep 17 00:00:00 2001 From: alse Date: Mon, 8 Jun 2020 23:49:00 +0530 Subject: [PATCH] Handle case where there is invalid cron time expression --- app.js | 2 +- crontab.js | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 23a7de9..c9309d2 100755 --- a/app.js +++ b/app.js @@ -215,7 +215,7 @@ app.use(function(err, req, res, next) { data.stack = err.stack; } - if (parseInt(data.statusCode) >= 500) { + if (statusCode >= 500) { console.error(err); } diff --git a/crontab.js b/crontab.js index 5c0c1de..dbd701a 100644 --- a/crontab.js +++ b/crontab.js @@ -62,7 +62,12 @@ exports.crontabs = function(callback){ if(docs[i].schedule == "@reboot") docs[i].next = "Next Reboot"; else - docs[i].next = cron_parser.parseExpression(docs[i].schedule).next().toString(); + try { + docs[i].next = cron_parser.parseExpression(docs[i].schedule).next().toString(); + } catch(err) { + console.error(err); + docs[i].next = "invalid"; + } } callback(docs); }); @@ -129,11 +134,18 @@ exports.set_crontab = function(env_vars, callback){ }); fs.writeFile(exports.env_file, env_vars, function(err) { - if (err) callback(err); + if (err) { + console.error(err); + callback(err); + } // In docker we're running as the root user, so we need to write the file as root and not crontab var fileName = process.env.CRON_IN_DOCKER !== undefined ? "root" : "crontab"; fs.writeFile(path.join(cronPath, fileName), crontab_string, function(err) { - if (err) return callback(err); + if (err) { + console.error(err); + return callback(err); + } + exec("crontab " + path.join(cronPath, fileName), function(err) { if (err) { console.error(err);