From f78540c3fbf109d6ccf2d92c5b1cf0148c88f722 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 27 Oct 2019 13:51:53 +0100 Subject: Move note actions to their own file. Because of circular import problems, this commit also moves the error messages from response.js to errors.js Signed-off-by: David Mehren --- lib/errors.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/errors.js (limited to 'lib/errors.js') diff --git a/lib/errors.js b/lib/errors.js new file mode 100644 index 00000000..64f93859 --- /dev/null +++ b/lib/errors.js @@ -0,0 +1,38 @@ +const config = require('./config') + +module.exports = { + errorForbidden: function (res) { + const { req } = res + if (req.user) { + responseError(res, '403', 'Forbidden', 'oh no.') + } else { + req.flash('error', 'You are not allowed to access this page. Maybe try logging in?') + res.redirect(config.serverURL + '/') + } + }, + errorNotFound: function (res) { + responseError(res, '404', 'Not Found', 'oops.') + }, + errorBadRequest: function (res) { + responseError(res, '400', 'Bad Request', 'something not right.') + }, + errorTooLong: function (res) { + responseError(res, '413', 'Payload Too Large', 'Shorten your note!') + }, + errorInternalError: function (res) { + responseError(res, '500', 'Internal Error', 'wtf.') + }, + errorServiceUnavailable: function (res) { + res.status(503).send('I\'m busy right now, try again later.') + } +} + +function responseError (res, code, detail, msg) { + res.status(code).render('error.ejs', { + title: code + ' ' + detail + ' ' + msg, + code: code, + detail: detail, + msg: msg, + opengraph: [] + }) +} -- cgit v1.2.3