UNPKG

1.33 kBJavaScriptView Raw
1'use strict'
2
3const { kFourOhFourContext, kReplySerializerDefault } = 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, logSerializers, attachValidation, replySerializer) {
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.logSerializers = logSerializers
24 this[kFourOhFourContext] = null
25 this.attachValidation = attachValidation
26 this[kReplySerializerDefault] = replySerializer
27}
28
29function defaultErrorHandler (error, request, reply) {
30 var res = reply.res
31 if (res.statusCode >= 500) {
32 reply.log.error({ req: reply.request.raw, res: res, err: error }, error && error.message)
33 } else if (res.statusCode >= 400) {
34 reply.log.info({ res: res, err: error }, error && error.message)
35 }
36 reply.send(error)
37}
38
39module.exports = Context