created error.ts

- moved export
- fixed function order
- errorServiceUnavailable now uses responseError like everything else

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
Philip Molares 2020-04-12 15:59:25 +02:00 committed by David Mehren
parent 6601ac76b5
commit 8b9c639e89
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
2 changed files with 54 additions and 40 deletions

View file

@ -1,40 +0,0 @@
const config = require('./config')
module.exports = {
errorForbidden: function (res) {
const { req } = res
if (req.user) {
responseError(res, '403', 'Forbidden', 'oh no.')
} else {
if (!req.session) req.session = {}
req.session.returnTo = req.originalUrl || config.serverUrl + '/'
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: []
})
}

54
lib/errors.ts Normal file
View file

@ -0,0 +1,54 @@
const config = require('./config')
function responseError (res, code: number, detail: string, msg: string): void {
res.status(code).render('error.ejs', {
title: code + ' ' + detail + ' ' + msg,
code: code,
detail: detail,
msg: msg,
opengraph: []
})
}
function errorForbidden (res): void {
const { req } = res
if (req.user) {
responseError(res, 403, 'Forbidden', 'oh no.')
} else {
if (!req.session) req.session = {}
req.session.returnTo = req.originalUrl || config.serverUrl + '/'
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
res.redirect(config.serverURL + '/')
}
}
function errorNotFound (res): void {
responseError(res, 404, 'Not Found', 'oops.')
}
function errorBadRequest (res): void {
responseError(res, 400, 'Bad Request', 'something not right.')
}
function errorTooLong (res): void {
responseError(res, 413, 'Payload Too Large', 'Shorten your note!')
}
function errorInternalError (res): void {
responseError(res, 500, 'Internal Error', 'wtf.')
}
function errorServiceUnavailable (res): void {
responseError(res, 503, 'Service Unvavilable', 'I\'m busy right now, try again later.')
}
const errors = {
errorForbidden: errorForbidden,
errorNotFound: errorNotFound,
errorBadRequest: errorBadRequest,
errorTooLong: errorTooLong,
errorInternalError: errorInternalError,
errorServiceUnavailable: errorServiceUnavailable
}
export { errors }