-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
87 lines (70 loc) · 2.03 KB
/
index.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict'
/**
* Module dependencies.
*/
const consolidate = require('consolidate')
const { join } = require('path')
const http = require('http')
/**
* Expose `error`.
*/
module.exports = error
/**
* Error middleware.
*
* - `template` defaults to ./error.html
*
* @param {Object} opts
* @api public
*/
function error (opts) {
opts = opts || {}
const engine = opts.engine || 'lodash'
const accepts = opts.accepts || [ 'html', 'text', 'json' ]
// template
const path = opts.template || join(__dirname, '/error.html')
// env
const env = opts.env || process.env.NODE_ENV || 'development'
var cache = opts.cache
if (cache == null) cache = env !== 'development'
return async function error (ctx, next) {
try {
await next()
if (ctx.response.status === 404 && !ctx.response.body) ctx.throw(404)
} catch (err) {
ctx.status = typeof err.status === 'number' ? err.status : 500
// application
ctx.app.emit('error', err, ctx)
// accepted types
switch (ctx.accepts.apply(ctx, accepts)) {
case 'text':
ctx.type = 'text/plain'
if (env === 'development') ctx.body = err.message
else if (err.expose) ctx.body = err.message
else throw err
break
case 'json':
ctx.type = 'application/json'
if (env === 'development') ctx.body = { error: err.message, stack: err.stack, originalError: err }
else if (err.expose) ctx.body = { error: err.message, originalError: err }
else ctx.body = { error: http.STATUS_CODES[ctx.status] }
break
case 'html':
ctx.type = 'text/html'
ctx.body = await consolidate[engine](path, {
originalError: err,
cache: cache,
env: env,
ctx: ctx,
request: ctx.request,
response: ctx.response,
error: err.message,
stack: err.stack,
status: ctx.status,
code: err.code
})
break
}
}
}
}