From 40c7008d1aea56cdf80b5ec3e21b50d72d679851 Mon Sep 17 00:00:00 2001 From: Joerg Henning Date: Thu, 29 Sep 2016 10:48:57 +0530 Subject: [PATCH] Proper, node-js style error handling for set_crontab It's a start... --- app.js | 34 +++++++++++++++++++++++++++------- crontab.js | 29 ++++++++++++++++++++--------- public/js/script.js | 11 +++++++++++ 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/app.js b/app.js index 113fbf8..3087b38 100755 --- a/app.js +++ b/app.js @@ -72,9 +72,11 @@ app.post(routes.remove, function(req, res) { crontab.remove(req.body._id); res.end(); }); -app.get(routes.crontab, function(req, res) { - crontab.set_crontab(req.query.env_vars); - res.end(); +app.get(routes.crontab, function(req, res, next) { + crontab.set_crontab(req.query.env_vars, function(err) { + if (err) next(err); + else res.end(); + }); }); app.get(routes.backup, function(req, res) { @@ -127,8 +129,8 @@ app.post(routes.import, function(req, res) { fstream.on('close', function () { crontab.reload_db(); res.redirect(routes.root); - }); - }); + }); + }); }); app.get(routes.import_crontab, function(req, res) { @@ -145,6 +147,24 @@ app.get(routes.logger, function(req, res) { res.end("No errors logged yet"); }); -app.listen(app.get('port'), function() { - console.log("Crontab UI is running at http://localhost:" + app.get('port')); +// error handler +app.use(function(err, req, res, next) { + var data = {}; + var statusCode = err.statusCode || 500; + + data.message = err.message || 'Internal Server Error'; + + if (process.env.NODE_ENV === 'development' && err.stack) { + data.stack = err.stack + } + + if (parseInt(data.statusCode) >= 500) { + console.error(err); + } + + res.status(statusCode).json(data); +}); + +app.listen(app.get('port'), function() { + console.log("Crontab UI is running at http://localhost:" + app.get('port')); }); diff --git a/crontab.js b/crontab.js index 253c760..b6f743f 100644 --- a/crontab.js +++ b/crontab.js @@ -1,8 +1,11 @@ //load database var Datastore = require('nedb'); var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' }); + db.loadDatabase(function (err) { + if (err) throw err; // no hope, just terminate }); + var exec = require('child_process').exec; var fs = require('fs'); var cron_parser = require("cron-parser"); @@ -52,32 +55,40 @@ exports.crontabs = function(callback){ callback(docs); }); }; -exports.set_crontab = function(env_vars){ +exports.set_crontab = function(env_vars, callback){ exports.crontabs( function(tabs){ var crontab_string = ""; if (env_vars) { crontab_string = env_vars + "\n"; } tabs.forEach(function(tab){ - if(!tab.stopped){ - if (tab.logging && tab.logging == "true"){ + if(!tab.stopped) { + if (tab.logging && tab.logging == "true") { tmp_log = "/tmp/" + tab._id + ".log"; log_file = exports.log_folder + "/" + tab._id + ".log"; if(tab.command[tab.command.length-1] != ";") // add semicolon tab.command +=";"; //{ command; } 2>/tmp/.log|| {if test -f /tmp/; then date >> ; cat /tmp/.log >> ; rm /tmp.log } crontab_string += tab.schedule + " { " + tab.command + " } 2> " + tmp_log +"; if test -f " + tmp_log +"; then date >> " + log_file + "; cat " + tmp_log + " >> " + log_file + "; rm " + tmp_log + "; fi \n"; - } - else + } + else { crontab_string += tab.schedule + " " + tab.command + "\n"; + } } }); - fs.writeFile(exports.env_file, env_vars); - fs.writeFile("/tmp/crontab", crontab_string, function(err) { - exec("crontab /tmp/crontab"); - }); + fs.writeFile(exports.env_file, env_vars, function(err) { + if (err) callback(err); + fs.writeFile("/tmp/crontab", crontab_string, function(err) { + if (err) return callback(err); + + exec("crontab /tmp/crontab", function(err) { + if (err) return callback(err); + else callback(); + }); + }); + }); }); }; diff --git a/public/js/script.js b/public/js/script.js index c704a95..174b2d3 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -6,6 +6,15 @@ function infoMessageBox(message, title){ $("#info-title").html(title); $("#info-popup").modal('show'); } +// like info, but for errors. +function errorMessageBox(message) { + var msg = + "Operation failed: " + message + ". " + + "Please see error log for details."; + $("#info-body").html(msg); + $("#info-title").html("Error"); + $("#info-popup").modal('show'); +} // modal with full control function messageBox(body, title, ok_text, close_text, callback){ $("#modal-body").html(body); @@ -50,6 +59,8 @@ function setCrontab(){ $.get(routes.crontab, { "env_vars": $("#env_vars").val() }, function(){ // TODO show only if success infoMessageBox("Successfuly set crontab file!","Information"); + }).fail(function(response) { + errorMessageBox(response.statusText,"Error"); }); }); }