UNPKG

1.18 kBJavaScriptView Raw
1'use strict'
2
3const { kFourOhFourContext } = require('./symbols.js')
4
5// Objects that holds the context of every request
6// Every route holds an instance of this object.
7function Context (schema, handler, Reply, Request, contentTypeParser, config, errorHandler, bodyLimit, logLevel, attachValidation) {
8 this.schema = schema
9 this.handler = handler
10 this.Reply = Reply
11 this.Request = Request
12 this.contentTypeParser = contentTypeParser
13 this.onRequest = null
14 this.onSend = null
15 this.onError = null
16 this.preHandler = null
17 this.onResponse = null
18 this.config = config
19 this.errorHandler = errorHandler || defaultErrorHandler
20 this._middie = null
21 this._parserOptions = { limit: bodyLimit || null }
22 this.logLevel = logLevel
23 this[kFourOhFourContext] = null
24 this.attachValidation = attachValidation
25}
26
27function defaultErrorHandler (error, request, reply) {
28 var res = reply.res
29 if (res.statusCode >= 500) {
30 res.log.error({ req: reply.request.raw, res: res, err: error }, error && error.message)
31 } else if (res.statusCode >= 400) {
32 res.log.info({ res: res, err: error }, error && error.message)
33 }
34 reply.send(error)
35}
36
37module.exports = Context