From c1aa8db8153459025851b6b64f734926ba0111c0 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 24 Jan 2013 20:51:38 +0000 Subject: [PATCH] fix db loading logic and check the file exists before attempting to load [#163] --- run.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/run.js b/run.js index 1807d04..9d3deb4 100644 --- a/run.js +++ b/run.js @@ -6,23 +6,19 @@ require('./snippets'); var DBot = function(timers) { // Load DB - var rawDB; - try { - var rawDB = fs.readFileSync('db.json', 'utf-8'); - } catch(err) { - this.db = {}; // If no db file, make empty one + if(fs.existsSync('db.json')) { + try { + this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); + } catch(err) { + console.log('Error loading db.json. Stopping: ' + err); + process.exit(); + } + } else { + this.db = {}; } - try { - if(!this.db) { // If it wasn't empty - this.db = JSON.parse(rawDB); - } - if(!_.has(this.db, 'config')) { - this.db.config = {}; - } - } catch(err) { - console.log('Syntax error in db.json. Stopping: ' + err); - process.exit(); + if(!_.has(this.db, 'config')) { + this.db.config = {}; } // Load config