Home / Function/ handleError() — fastify Function Reference

handleError() — fastify Function Reference

Architecture documentation for the handleError() function in error-handler.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  10359811_0bd9_8a13_f97b_f71d10e1c7d5["handleError()"]
  4fa9719b_0e99_aac6_5850_8ee791f4f088["error-handler.js"]
  10359811_0bd9_8a13_f97b_f71d10e1c7d5 -->|defined in| 4fa9719b_0e99_aac6_5850_8ee791f4f088
  87b3d7c9_fba2_741d_abf0_b873e921c26f["fallbackErrorHandler()"]
  10359811_0bd9_8a13_f97b_f71d10e1c7d5 -->|calls| 87b3d7c9_fba2_741d_abf0_b873e921c26f
  style 10359811_0bd9_8a13_f97b_f71d10e1c7d5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/error-handler.js lines 30–80

function handleError (reply, error, cb) {
  reply[kReplyIsRunningOnErrorHook] = false

  const context = reply[kRouteContext]
  if (reply[kReplyNextErrorHandler] === false) {
    fallbackErrorHandler(error, reply, function (reply, payload) {
      try {
        reply.raw.writeHead(reply.raw.statusCode, reply[kReplyHeaders])
      } catch (error) {
        if (!reply.log[kDisableRequestLogging]) {
          reply.log.warn(
            { req: reply.request, res: reply, err: error },
            error?.message
          )
        }
        reply.raw.writeHead(reply.raw.statusCode)
      }
      reply.raw.end(payload)
    })
    return
  }
  const errorHandler = reply[kReplyNextErrorHandler] || context.errorHandler

  // In case the error handler throws, we set the next errorHandler so we can error again
  reply[kReplyNextErrorHandler] = Object.getPrototypeOf(errorHandler)

  // we need to remove content-type to allow content-type guessing for serialization
  delete reply[kReplyHeaders]['content-type']
  delete reply[kReplyHeaders]['content-length']

  const func = errorHandler.func

  if (!func) {
    reply[kReplyNextErrorHandler] = false
    fallbackErrorHandler(error, reply, cb)
    return
  }

  try {
    const result = func(error, reply.request, reply)
    if (result !== undefined) {
      if (result !== null && typeof result.then === 'function') {
        wrapThenable(result, reply)
      } else {
        reply.send(result)
      }
    }
  } catch (err) {
    reply.send(err)
  }
}

Domain

Subdomains

Frequently Asked Questions

What does handleError() do?
handleError() is a function in the fastify codebase, defined in lib/error-handler.js.
Where is handleError() defined?
handleError() is defined in lib/error-handler.js at line 30.
What does handleError() call?
handleError() calls 1 function(s): fallbackErrorHandler.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free