diff options
author | Sheogorath | 2019-11-20 20:07:35 +0100 |
---|---|---|
committer | GitHub | 2019-11-20 20:07:35 +0100 |
commit | 689f5a0a9583fdd774a271a9e6265ee5356d72a0 (patch) | |
tree | 754bed08f4724a4bc4f91743f6dfe12f92857c01 /lib/errors.js | |
parent | f894d3c2fa29865288a31cd80cd2a772ce44b330 (diff) | |
parent | b5ccceff59002034fbb089935076f40b8aa16e58 (diff) |
Merge pull request #213 from davidmehren/refactor_backend_notes
First steps in refactoring the backend code
Diffstat (limited to 'lib/errors.js')
-rw-r--r-- | lib/errors.js | 38 |
1 files changed, 38 insertions, 0 deletions
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: [] + }) +} |