From e188723c9a2a9dd0dc5e2c5debc2b20361f35f2e Mon Sep 17 00:00:00 2001 From: joates Date: Mon, 22 Sep 2014 11:23:41 +0100 Subject: [PATCH] v0.4.2-rc2 (improved middleware error handling) --- package.json | 2 +- server.js | 6 ++++++ src/email-confirm.js | 38 ++++++++++++++++++++++++-------------- src/email-submit.js | 13 +++++++++---- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index a9785bd..f5913f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "squatconf-web", - "version": "0.4.1", + "version": "0.4.2", "description": "website for the squatConf conference", "main": "server.js", "scripts": { diff --git a/server.js b/server.js index d933990..267e133 100644 --- a/server.js +++ b/server.js @@ -18,6 +18,12 @@ if(!fs.existsSync(config.db_path)){ }); } +stack.errorHandler = function error(req, res, err) { + res.statusCode = 302 + res.setHeader('Location', '/') + res.end() +} + var app = stack( route('/email', require('./src/email-submit')(db)) , route('/confirm', require('./src/email-confirm')(db)) diff --git a/src/email-confirm.js b/src/email-confirm.js index eaba7de..17f6cd1 100644 --- a/src/email-confirm.js +++ b/src/email-confirm.js @@ -5,6 +5,11 @@ module.exports = function(db) { return function (req, res, next) { req.resume() + function error(e) { + console.error(e) + return next(e || e.msg || 'ERROR') + } + var params = require('url').parse(req.url, true) if (params && params.query.email && params.query.token) { @@ -13,25 +18,30 @@ module.exports = function(db) { , token = sanitize(params.query.token) db.get(email, function(err, obj) { - if (err) return console.error(err) + if (err) return error(err) // db read OK.. - if (obj && obj.token === token) { - obj.verified = true - obj.trace = obj.trace.concat(ip(req)) - - db.put(email, obj, function(err) { - if (err) return console.error(err) - - // db write OK.. - res.statusCode = 302 - res.setHeader('Location', '/verified.html') - return res.end() - }) + if (obj && ! obj.verified) { + if (obj.token === token) { + obj.verified = true + obj.trace = obj.trace.concat(ip(req)) + + db.put(email, obj, function(err) { + if (err) return error(err) + + // db write OK.. + res.statusCode = 302 + res.setHeader('Location', '/verified.html') + return res.end() + }) + } + } else { + error('email is already verified: '+ email) } }) - if (next) return next() + } else { + error('invalid input: '+ JSON.stringify(params.query)) } } } diff --git a/src/email-submit.js b/src/email-submit.js index 781ffa5..9d65363 100644 --- a/src/email-submit.js +++ b/src/email-submit.js @@ -7,6 +7,11 @@ module.exports = function(db) { return function (req, res, next) { req.resume() + function error(e) { + console.error(e) + return next(e || e.msg || 'ERROR') + } + var params = require('url').parse(req.url, true) if (params && params.query.email) { @@ -22,7 +27,7 @@ module.exports = function(db) { obj.trace = ip(req) db.put(email, obj, function(err) { - if (err) return console.error(err) + if (err) return error(err) // db write OK.. var nodemailer = require('nodemailer') @@ -39,7 +44,7 @@ module.exports = function(db) { } transporter.sendMail(opts, function(err, data) { - if (err) return console.error(err) + if (err) return error(err) // validation email sent console.log('email sent..', opts) @@ -49,9 +54,9 @@ module.exports = function(db) { return res.end() }) }) + } else { + error('invalid input: '+ JSON.stringify(params.query)) } - - if (next) return next() } }