2019-10-27 08:51:53 -04:00
|
|
|
const config = require('./config')
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
errorForbidden: function (res) {
|
|
|
|
const { req } = res
|
|
|
|
if (req.user) {
|
2022-10-30 16:57:16 -04:00
|
|
|
responseError(res, 403, 'Forbidden', 'oh no.')
|
2019-10-27 08:51:53 -04:00
|
|
|
} else {
|
2019-11-28 04:59:59 -05:00
|
|
|
if (!req.session) req.session = {}
|
2020-02-25 04:58:10 -05:00
|
|
|
if (req.originalUrl !== '/403') {
|
|
|
|
req.session.returnTo = config.serverURL + (req.originalUrl || '/')
|
|
|
|
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
|
|
|
|
}
|
2019-10-27 08:51:53 -04:00
|
|
|
res.redirect(config.serverURL + '/')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
errorNotFound: function (res) {
|
2022-10-30 16:57:16 -04:00
|
|
|
responseError(res, 404, 'Not Found', 'oops.')
|
2019-10-27 08:51:53 -04:00
|
|
|
},
|
|
|
|
errorBadRequest: function (res) {
|
2022-10-30 16:57:16 -04:00
|
|
|
responseError(res, 400, 'Bad Request', 'something not right.')
|
2019-10-27 08:51:53 -04:00
|
|
|
},
|
2021-03-29 17:00:34 -04:00
|
|
|
errorConflict: function (res) {
|
2022-10-30 16:57:16 -04:00
|
|
|
responseError(res, 409, 'Conflict', 'This note already exists.')
|
2021-03-29 17:00:34 -04:00
|
|
|
},
|
2019-10-27 08:51:53 -04:00
|
|
|
errorTooLong: function (res) {
|
2022-10-30 16:57:16 -04:00
|
|
|
responseError(res, 413, 'Payload Too Large', 'Shorten your note!')
|
2019-10-27 08:51:53 -04:00
|
|
|
},
|
|
|
|
errorInternalError: function (res) {
|
2022-10-30 16:57:16 -04:00
|
|
|
responseError(res, 500, 'Internal Error', 'wtf.')
|
2019-10-27 08:51:53 -04:00
|
|
|
},
|
|
|
|
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,
|
2022-05-01 15:14:27 -04:00
|
|
|
code,
|
|
|
|
detail,
|
|
|
|
msg,
|
2019-10-27 08:51:53 -04:00
|
|
|
opengraph: []
|
|
|
|
})
|
|
|
|
}
|