MCPcopy
hub / github.com/fastify/fastify / handleError

Function handleError

lib/error-handler.js:30–81  ·  view source on GitHub ↗
(reply, error, cb)

Source from the content-addressed store, hash-verified

28}
29
30function handleError (reply, error, cb) {
31 reply[kReplyIsRunningOnErrorHook] = false
32
33 const context = reply[kRouteContext]
34 if (reply[kReplyNextErrorHandler] === false) {
35 fallbackErrorHandler(error, reply, function (reply, payload) {
36 try {
37 reply.raw.writeHead(reply.raw.statusCode, reply[kReplyHeaders])
38 } catch (error) {
39 if (!reply.log[kDisableRequestLogging]) {
40 reply.log.warn(
41 { req: reply.request, res: reply, err: error },
42 error?.message
43 )
44 }
45 reply.raw.writeHead(reply.raw.statusCode)
46 }
47 reply.raw.end(payload)
48 })
49 return
50 }
51 const errorHandler = reply[kReplyNextErrorHandler] || context.errorHandler
52
53 // In case the error handler throws, we set the next errorHandler so we can error again
54 reply[kReplyNextErrorHandler] = Object.getPrototypeOf(errorHandler)
55
56 // we need to remove content-type to allow content-type guessing for serialization
57 delete reply[kReplyHeaders]['content-type']
58 delete reply[kReplyHeaders]['content-length']
59
60 const func = errorHandler.func
61
62 if (!func) {
63 reply[kReplyNextErrorHandler] = false
64 fallbackErrorHandler(error, reply, cb)
65 return
66 }
67
68 try {
69 const result = func(error, reply.request, reply)
70 if (result !== undefined) {
71 if (result !== null && typeof result.then === 'function') {
72 const store = reply[kDiagnosticsStore] || null
73 wrapThenable(result, reply, store)
74 } else {
75 reply.send(result)
76 }
77 }
78 } catch (err) {
79 reply.send(err)
80 }
81}
82
83function defaultErrorHandler (error, request, reply) {
84 setErrorHeaders(error, reply)

Callers 1

onErrorHookFunction · 0.85

Calls 5

fallbackErrorHandlerFunction · 0.85
funcFunction · 0.85
wrapThenableFunction · 0.85
warnMethod · 0.80
sendMethod · 0.80

Tested by

no test coverage detected