MCPcopy Create free account
hub / github.com/fastify/fastify / handleError

Function handleError

lib/error-handler.js:30–76  ·  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 reply.server[kLogController].writeHeadError(error, reply.request, reply)
40 reply.raw.writeHead(reply.raw.statusCode)
41 }
42 reply.raw.end(payload)
43 })
44 return
45 }
46 const errorHandler = reply[kReplyNextErrorHandler] || context.errorHandler
47
48 // In case the error handler throws, we set the next errorHandler so we can error again
49 reply[kReplyNextErrorHandler] = Object.getPrototypeOf(errorHandler)
50
51 // we need to remove content-type to allow content-type guessing for serialization
52 delete reply[kReplyHeaders]['content-type']
53 delete reply[kReplyHeaders]['content-length']
54
55 const func = errorHandler.func
56
57 if (!func) {
58 reply[kReplyNextErrorHandler] = false
59 fallbackErrorHandler(error, reply, cb)
60 return
61 }
62
63 try {
64 const result = func(error, reply.request, reply)
65 if (result !== undefined) {
66 if (result !== null && typeof result.then === 'function') {
67 const store = reply[kDiagnosticsStore] || null
68 wrapThenable(result, reply, store)
69 } else {
70 reply.send(result)
71 }
72 }
73 } catch (err) {
74 reply.send(err)
75 }
76}
77
78function defaultErrorHandler (error, request, reply) {
79 setErrorHeaders(error, reply)

Callers 1

onErrorHookFunction · 0.85

Calls 5

fallbackErrorHandlerFunction · 0.85
funcFunction · 0.85
wrapThenableFunction · 0.85
writeHeadErrorMethod · 0.80
sendMethod · 0.80

Tested by

no test coverage detected