summaryrefslogtreecommitdiff
path: root/lib/errors.js
blob: 599f54b25e2cd7b8271481bd7ef99f1826ece433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 = {}
      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?')
      }
      res.redirect(config.serverURL + '/')
    }
  },
  errorNotFound: function (res) {
    responseError(res, '404', 'Not Found', 'oops.')
  },
  errorBadRequest: function (res) {
    responseError(res, '400', 'Bad Request', 'something not right.')
  },
  errorConflict: function (res) {
    responseError(res, '409', 'Conflict', 'This note already exists.')
  },
  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: []
  })
}