MCPcopy
hub / github.com/fastify/fastify / fallbackErrorHandler

Function fallbackErrorHandler

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

Source from the content-addressed store, hash-verified

102}
103
104function fallbackErrorHandler (error, reply, cb) {
105 const res = reply.raw
106 const statusCode = reply.statusCode
107 reply[kReplyHeaders]['content-type'] = reply[kReplyHeaders]['content-type'] ?? 'application/json; charset=utf-8'
108 let payload
109 try {
110 const serializerFn = getSchemaSerializer(reply[kRouteContext], statusCode, reply[kReplyHeaders]['content-type'])
111 if (serializerFn === false) {
112 payload = serializeError({
113 error: statusCodes[statusCode + ''],
114 code: error.code,
115 message: error.message,
116 statusCode
117 })
118 } else {
119 payload = serializerFn(Object.create(error, {
120 error: { value: statusCodes[statusCode + ''] },
121 message: { value: error.message },
122 statusCode: { value: statusCode }
123 }))
124 }
125 } catch (err) {
126 if (!reply.log[kDisableRequestLogging]) {
127 // error is always FST_ERR_SCH_SERIALIZATION_BUILD because this is called from route/compileSchemasForSerialization
128 reply.log.error({ err, statusCode: res.statusCode }, 'The serializer for the given status code failed')
129 }
130 reply.code(500)
131 payload = serializeError(new FST_ERR_FAILED_ERROR_SERIALIZATION(err.message, error.message))
132 }
133
134 if (typeof payload !== 'string' && !Buffer.isBuffer(payload)) {
135 payload = serializeError(new FST_ERR_REP_INVALID_PAYLOAD_TYPE(typeof payload))
136 }
137
138 reply[kReplyHeaders]['content-length'] = '' + Buffer.byteLength(payload)
139
140 cb(reply, payload)
141}
142
143function buildErrorHandler (parent = rootErrorHandler, func) {
144 if (!func) {

Callers 1

handleErrorFunction · 0.85

Calls 4

getSchemaSerializerFunction · 0.85
cbFunction · 0.85
errorMethod · 0.80
codeMethod · 0.80

Tested by

no test coverage detected