Skip to content

Node.js based forum software built for the modern web

error handler tests

Barış Soner Uşaklı608a7d120c3a
diff --git a/src/controllers/errors.js b/src/controllers/errors.js
index 0e61b557cc..99614f9e87 100644
--- a/src/controllers/errors.js
+++ b/src/controllers/errors.js
@@ -16,14 +16,14 @@ exports.handleURIErrors = function (err, req, res, next) {
res.redirect(nconf.get('relative_path') + cidMatch[0]);
} else {
winston.warn('[controller] Bad request: ' + req.path);
- if (res.locals.isAPI) {
+ if (req.path.startsWith(nconf.get('relative_path') + '/api')) {
res.status(400).json({
error: '[[global:400.title]]',
});
} else {
var middleware = require('../middleware');
middleware.buildHeader(req, res, function () {
- res.render('400', { error: validator.escape(String(err.message)) });
+ res.status(400).render('400', { error: validator.escape(String(err.message)) });
});
}
}
diff --git a/src/middleware/index.js b/src/middleware/index.js
index de0bd6797e..d4cdb0bfd2 100644
--- a/src/middleware/index.js
+++ b/src/middleware/index.js
@@ -122,9 +122,9 @@ middleware.pageView = function (req, res, next) {
middleware.pluginHooks = function (req, res, next) {
async.each(plugins.loadedHooks['filter:router.page'] || [], function (hookObj, next) {
hookObj.method(req, res, next);
- }, function () {
+ }, function (err) {
// If it got here, then none of the subscribed hooks did anything, or there were no hooks
- next();
+ next(err);
});
};
diff --git a/test/controllers.js b/test/controllers.js
index 3e1000e39f..5c0f143758 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -1069,6 +1069,143 @@ describe('Controllers', function () {
});
});
+ describe('handle errors', function () {
+ var plugins = require('../src/plugins');
+ after(function (done) {
+ plugins.loadedHooks['filter:router.page'] = undefined;
+ done();
+ });
+
+ it('should handle topic malformed uri', function (done) {
+ request(nconf.get('url') + '/topic/1/a%AFc', function (err, res, body) {
+ assert.ifError(err);
+ assert(body);
+ done();
+ });
+ });
+
+ it('should handle category malformed uri', function (done) {
+ request(nconf.get('url') + '/category/1/a%AFc', function (err, res, body) {
+ assert.ifError(err);
+ assert(body);
+ done();
+ });
+ });
+
+ it('should handle malformed uri ', function (done) {
+ request(nconf.get('url') + '/user/a%AFc', function (err, res, body) {
+ assert.ifError(err);
+ assert(body);
+ assert.equal(res.statusCode, 400);
+ done();
+ });
+ });
+
+ it('should handle malformed uri in api', function (done) {
+ request(nconf.get('url') + '/api/user/a%AFc', { json: true }, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 400);
+ assert.equal(body.error, '[[global:400.title]]');
+ done();
+ });
+ });
+
+ it('should handle CSRF error', function (done) {
+ plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || [];
+ plugins.loadedHooks['filter:router.page'].push({
+ method: function (req, res, next) {
+ var err = new Error('csrf-error');
+ err.code = 'EBADCSRFTOKEN';
+ next(err);
+ },
+ });
+
+ request(nconf.get('url') + '/users', { }, function (err, res, body) {
+ plugins.loadedHooks['filter:router.page'] = [];
+ assert.ifError(err);
+ assert.equal(res.statusCode, 403);
+ done();
+ });
+ });
+
+ it('should handle black-list error', function (done) {
+ plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || [];
+ plugins.loadedHooks['filter:router.page'].push({
+ method: function (req, res, next) {
+ var err = new Error('blacklist error message');
+ err.code = 'blacklisted-ip';
+ next(err);
+ },
+ });
+
+ request(nconf.get('url') + '/users', { }, function (err, res, body) {
+ plugins.loadedHooks['filter:router.page'] = [];
+ assert.ifError(err);
+ assert.equal(res.statusCode, 403);
+ assert.equal(body, 'blacklist error message');
+ done();
+ });
+ });
+
+ it('should handle page redirect through error', function (done) {
+ plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || [];
+ plugins.loadedHooks['filter:router.page'].push({
+ method: function (req, res, next) {
+ var err = new Error('redirect');
+ err.status = 302;
+ err.path = '/popular';
+ plugins.loadedHooks['filter:router.page'] = [];
+ next(err);
+ },
+ });
+
+ request(nconf.get('url') + '/users', { }, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(body);
+ done();
+ });
+ });
+
+ it('should handle api page redirect through error', function (done) {
+ plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || [];
+ plugins.loadedHooks['filter:router.page'].push({
+ method: function (req, res, next) {
+ var err = new Error('redirect');
+ err.status = 308;
+ err.path = '/api/popular';
+ plugins.loadedHooks['filter:router.page'] = [];
+ next(err);
+ },
+ });
+
+ request(nconf.get('url') + '/api/users', { json: true }, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 308);
+ assert(body, '/api/popular');
+ done();
+ });
+ });
+
+ it('should handle error page', function (done) {
+ plugins.loadedHooks['filter:router.page'] = plugins.loadedHooks['filter:router.page'] || [];
+ plugins.loadedHooks['filter:router.page'].push({
+ method: function (req, res, next) {
+ var err = new Error('regular error');
+ next(err);
+ },
+ });
+
+ request(nconf.get('url') + '/users', function (err, res, body) {
+ plugins.loadedHooks['filter:router.page'] = [];
+ assert.ifError(err);
+ assert.equal(res.statusCode, 500);
+ assert(body);
+ done();
+ });
+ });
+ });
+
after(function (done) {
var analytics = require('../src/analytics');