MCPcopy
hub / github.com/fastify/fastify / setNotFoundHandler

Function setNotFoundHandler

lib/four-oh-four.js:86–141  ·  lib/four-oh-four.js::setNotFoundHandler
(opts, handler, avvio, routeHandler)

Source from the content-addressed store, hash-verified

84 }
85
86 function setNotFoundHandler (opts, handler, avvio, routeHandler) {
87 class="cm">// First initialization of the fastify root instance
88 if (this[kCanSetNotFoundHandler] === undefined) {
89 this[kCanSetNotFoundHandler] = true
90 }
91 if (this[kFourOhFourContext] === undefined) {
92 this[kFourOhFourContext] = null
93 }
94
95 const _fastify = this
96 const prefix = this[kRoutePrefix] || class="st">'/'
97
98 if (this[kCanSetNotFoundHandler] === false) {
99 throw new Error(`Not found handler already set for Fastify instance with prefix: class="st">'${prefix}'`)
100 }
101
102 if (typeof opts === class="st">'object') {
103 if (opts.preHandler) {
104 if (Array.isArray(opts.preHandler)) {
105 opts.preHandler = opts.preHandler.map(hook => hook.bind(_fastify))
106 } else {
107 opts.preHandler = opts.preHandler.bind(_fastify)
108 }
109 }
110
111 if (opts.preValidation) {
112 if (Array.isArray(opts.preValidation)) {
113 opts.preValidation = opts.preValidation.map(hook => hook.bind(_fastify))
114 } else {
115 opts.preValidation = opts.preValidation.bind(_fastify)
116 }
117 }
118 }
119
120 if (typeof opts === class="st">'function') {
121 handler = opts
122 opts = undefined
123 }
124 opts = opts || {}
125
126 if (handler) {
127 this[kFourOhFourLevelInstance][kCanSetNotFoundHandler] = false
128 handler = handler.bind(this)
129 class="cm">// update route event handler
130 _routeEventHandler = handler
131 } else {
132 handler = basic404
133 class="cm">// update route event handler
134 _routeEventHandler = basic404
135 }
136
137 this.after((notHandledErr, done) => {
138 _setNotFoundHandler.call(this, prefix, opts, handler, avvio, routeHandler)
139 done(notHandledErr)
140 })
141 }
142
143 function _setNotFoundHandler (prefix, opts, handler, avvio, routeHandler) {

Callers

nothing calls this directly

Calls 2

afterMethod · 0.80
doneFunction · 0.50

Tested by

no test coverage detected