move out error and 404 controllers
parent
284485c885
commit
bc8d297377
@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
var nconf = require('nconf');
|
||||
var winston = require('winston');
|
||||
var validator = require('validator');
|
||||
|
||||
var meta = require('../meta');
|
||||
var plugins = require('../plugins');
|
||||
|
||||
exports.handle404 = function (req, res) {
|
||||
var relativePath = nconf.get('relative_path');
|
||||
var isClientScript = new RegExp('^' + relativePath + '\\/assets\\/src\\/.+\\.js');
|
||||
|
||||
if (plugins.hasListeners('action:meta.override404')) {
|
||||
return plugins.fireHook('action:meta.override404', {
|
||||
req: req,
|
||||
res: res,
|
||||
error: {},
|
||||
});
|
||||
}
|
||||
|
||||
if (isClientScript.test(req.url)) {
|
||||
res.type('text/javascript').status(200).send('');
|
||||
} else if (req.path.startsWith(relativePath + '/assets/uploads') || (req.get('accept') && req.get('accept').indexOf('text/html') === -1) || req.path === '/favicon.ico') {
|
||||
meta.errors.log404(req.path || '');
|
||||
res.sendStatus(404);
|
||||
} else if (req.accepts('html')) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn('Route requested but not found: ' + req.url);
|
||||
}
|
||||
|
||||
meta.errors.log404(req.path.replace(/^\/api/, '') || '');
|
||||
res.status(404);
|
||||
|
||||
var path = String(req.path || '');
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
return res.json({ path: validator.escape(path.replace(/^\/api/, '')), title: '[[global:404.title]]' });
|
||||
}
|
||||
var middleware = require('../middleware');
|
||||
middleware.buildHeader(req, res, function () {
|
||||
res.render('404', { path: validator.escape(path), title: '[[global:404.title]]' });
|
||||
});
|
||||
} else {
|
||||
res.status(404).type('txt').send('Not found');
|
||||
}
|
||||
};
|
@ -0,0 +1,63 @@
|
||||
'use strict';
|
||||
|
||||
var nconf = require('nconf');
|
||||
var winston = require('winston');
|
||||
var validator = require('validator');
|
||||
|
||||
exports.handleURIErrors = function (err, req, res, next) {
|
||||
// Handle cases where malformed URIs are passed in
|
||||
if (err instanceof URIError) {
|
||||
var tidMatch = req.path.match(/^\/topic\/(\d+)\//);
|
||||
var cidMatch = req.path.match(/^\/category\/(\d+)\//);
|
||||
|
||||
if (tidMatch) {
|
||||
res.redirect(nconf.get('relative_path') + tidMatch[0]);
|
||||
} else if (cidMatch) {
|
||||
res.redirect(nconf.get('relative_path') + cidMatch[0]);
|
||||
} else {
|
||||
winston.warn('[controller] Bad request: ' + req.path);
|
||||
if (res.locals.isAPI) {
|
||||
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)) });
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
|
||||
// this needs to have four arguments or express treats it as `(req, res, next)`
|
||||
// don't remove `next`!
|
||||
exports.handleErrors = function (err, req, res, next) { // eslint-disable-line no-unused-vars
|
||||
switch (err.code) {
|
||||
case 'EBADCSRFTOKEN':
|
||||
winston.error(req.path + '\n', err.message);
|
||||
return res.sendStatus(403);
|
||||
case 'blacklisted-ip':
|
||||
return res.status(403).type('text/plain').send(err.message);
|
||||
}
|
||||
|
||||
if (parseInt(err.status, 10) === 302 && err.path) {
|
||||
return res.locals.isAPI ? res.status(302).json(err.path) : res.redirect(err.path);
|
||||
}
|
||||
|
||||
winston.error(req.path + '\n', err.stack);
|
||||
|
||||
res.status(err.status || 500);
|
||||
|
||||
var path = String(req.path || '');
|
||||
if (res.locals.isAPI) {
|
||||
res.json({ path: validator.escape(path), error: err.message });
|
||||
} else {
|
||||
var middleware = require('../middleware');
|
||||
middleware.buildHeader(req, res, function () {
|
||||
res.render('500', { path: validator.escape(path), error: validator.escape(String(err.message)) });
|
||||
});
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue