-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdefault-middleware.js
58 lines (47 loc) · 1.78 KB
/
default-middleware.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
var async = require('async')
var get = require('lodash.get')
var has = require('lodash.has')
var xml = require('jsontoxml')
var Boom = require('@hapi/boom')
var format = require('util').format
module.exports = function(options) {
var logger
var config
function init(dependencies, cb) {
config = dependencies.config || {}
app = dependencies.app
logger = dependencies.logger || app.locals.logger || console
cb()
}
function validate(cb) {
if (!app) return cb(new Error('app is required'))
cb()
}
function start(cb) {
app.use(get(options, 'handlers.notFound') || notFoundMiddleware);
app.use(get(options, 'handlers.error') || errorMiddleware);
cb(null, app)
}
function notFoundMiddleware(req, res, next) {
dispatch(Boom.notFound(), req, res)
}
function errorMiddleware(err, req, res, next) {
err = Boom.wrap(err)
// https://github.com/hapijs/boom/issues/39
if (config.showErrorDetail) {
err.output.payload.message = err.message
err.output.payload.stack = err.stack
}
has(res, 'locals.logger') ? res.locals.logger.log(err) : logger.log(err)
dispatch(err, req, res)
}
function dispatch(err, req, res) {
res.status(err.output.statusCode)
if (req.accepts('json')) res.set('Content-Type', 'application/json').send(JSON.stringify(err.output.payload) + '\n')
else if (req.accepts(['application/xml', 'text/xml'])) res.set('Content-Type', 'text/xml').send(xml(err.output.payload) + '\n')
else res.send(format('%s %s\n%s\n', err.output.payload.statusCode, err.output.payload.message, err.output.payload.stack || ''))
}
return {
start: async.seq(init, validate, start)
}
}