From cdad5bf8c2891ad76f7441fd4d8a74b058a14e6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Zanghelini?= Date: Wed, 5 Jul 2017 19:09:17 -0300 Subject: [PATCH] Update error handling --- src/controllers/authentication.js | 28 ++++++++-------------------- src/controllers/helpers.js | 4 ++++ 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js index 8aa609a32a..1a0abb5cf7 100644 --- a/src/controllers/authentication.js +++ b/src/controllers/authentication.js @@ -38,29 +38,20 @@ authenticationController.register = function (req, res) { } }, function (next) { - var err; - if (!userData.email) { - err = '[[error:invalid-email]]'; + return next(new Error('[[error:invalid-email]]')); } - if (!err && (!userData.username || userData.username.length < meta.config.minimumUsernameLength)) { - err = '[[error:username-too-short]]'; + if (!userData.username || userData.username.length < meta.config.minimumUsernameLength) { + return next(new Error('[[error:username-too-short]]')); } - if (!err && userData.username.length > meta.config.maximumUsernameLength) { - err = '[[error:username-too-long]]'; + if (userData.username.length > meta.config.maximumUsernameLength) { + return next(new Error('[[error:username-too-long]]')); } - if (!err && userData.password !== userData['password-confirm']) { - err = '[[user:change_password_error_match]]'; - } - - if (err) { - if (req.body.noscript === 'true') { - return helpers.noScriptErrors(req, res, err, 400); - } - return next(new Error(err)); + if (userData.password !== userData['password-confirm']) { + return next(new Error('[[user:change_password_error_match]]')); } user.isPasswordValid(userData.password, next); @@ -81,10 +72,7 @@ authenticationController.register = function (req, res) { }, ], function (err, data) { if (err) { - if (req.body.noscript === 'true') { - return helpers.noScriptErrors(req, res, err.message, 400); - } - return res.status(400).send(err.message); + return helpers.noScriptErrors(req, res, err.message, 400); } if (data.uid && req.body.userLang) { diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index 3057aa5265..b936bf3f8d 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -15,6 +15,10 @@ var middleware = require('../middleware'); var helpers = module.exports; helpers.noScriptErrors = function (req, res, error, httpStatus) { + if (req.body.noscript !== 'true') { + return res.status(httpStatus).send(error); + } + var middleware = require('../middleware'); var httpStatusString = httpStatus.toString(); middleware.buildHeader(req, res, function () {