1 | 'use strict'
|
2 |
|
3 | const { kFourOhFourContext, kReplySerializerDefault } = require('./symbols.js')
|
4 |
|
5 |
|
6 |
|
7 | function Context (schema, handler, Reply, Request, contentTypeParser, config, errorHandler, bodyLimit, logLevel, logSerializers, attachValidation, replySerializer, schemaErrorFormatter) {
|
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.onTimeout = null
|
17 | this.preHandler = null
|
18 | this.onResponse = null
|
19 | this.config = config
|
20 | this.errorHandler = errorHandler || defaultErrorHandler
|
21 | this._middie = null
|
22 | this._parserOptions = { limit: bodyLimit || null }
|
23 | this.logLevel = logLevel
|
24 | this.logSerializers = logSerializers
|
25 | this[kFourOhFourContext] = null
|
26 | this.attachValidation = attachValidation
|
27 | this[kReplySerializerDefault] = replySerializer
|
28 | this.schemaErrorFormatter = schemaErrorFormatter || defaultSchemaErrorFormatter
|
29 | }
|
30 |
|
31 | function defaultErrorHandler (error, request, reply) {
|
32 | if (reply.statusCode >= 500) {
|
33 | reply.log.error(
|
34 | { req: request, res: reply, err: error },
|
35 | error && error.message
|
36 | )
|
37 | } else if (reply.statusCode >= 400) {
|
38 | reply.log.info(
|
39 | { res: reply, err: error },
|
40 | error && error.message
|
41 | )
|
42 | }
|
43 | reply.send(error)
|
44 | }
|
45 |
|
46 | function defaultSchemaErrorFormatter (errors, dataVar) {
|
47 | var text = ''
|
48 | var separator = ', '
|
49 | for (var i = 0; i < errors.length; i++) {
|
50 | var e = errors[i]
|
51 | text += dataVar + (e.dataPath || '') + ' ' + e.message + separator
|
52 | }
|
53 | return new Error(text.slice(0, -separator.length))
|
54 | }
|
55 |
|
56 | module.exports = Context
|