Home / Function/ defaultClientErrorHandler() — fastify Function Reference

defaultClientErrorHandler() — fastify Function Reference

Architecture documentation for the defaultClientErrorHandler() function in fastify.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  b7187f8b_e1ca_c935_a50a_ce22faef4f11["defaultClientErrorHandler()"]
  2bf9986b_b7fb_0c78_f075_72fbe6f4672b["fastify.js"]
  b7187f8b_e1ca_c935_a50a_ce22faef4f11 -->|defined in| 2bf9986b_b7fb_0c78_f075_72fbe6f4672b
  style b7187f8b_e1ca_c935_a50a_ce22faef4f11 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastify.js lines 921–959

function defaultClientErrorHandler (err, socket) {
  // In case of a connection reset, the socket has been destroyed and there is nothing that needs to be done.
  // https://nodejs.org/api/http.html#http_event_clienterror
  if (err.code === 'ECONNRESET' || socket.destroyed) {
    return
  }

  let body, errorCode, errorStatus, errorLabel

  if (err.code === 'ERR_HTTP_REQUEST_TIMEOUT') {
    errorCode = '408'
    errorStatus = http.STATUS_CODES[errorCode]
    body = `{"error":"${errorStatus}","message":"Client Timeout","statusCode":408}`
    errorLabel = 'timeout'
  } else if (err.code === 'HPE_HEADER_OVERFLOW') {
    errorCode = '431'
    errorStatus = http.STATUS_CODES[errorCode]
    body = `{"error":"${errorStatus}","message":"Exceeded maximum allowed HTTP header size","statusCode":431}`
    errorLabel = 'header_overflow'
  } else {
    errorCode = '400'
    errorStatus = http.STATUS_CODES[errorCode]
    body = `{"error":"${errorStatus}","message":"Client Error","statusCode":400}`
    errorLabel = 'error'
  }

  // Most devs do not know what to do with this error.
  // In the vast majority of cases, it's a network error and/or some
  // config issue on the load balancer side.
  this.log.trace({ err }, `client ${errorLabel}`)
  // Copying standard node behavior
  // https://github.com/nodejs/node/blob/6ca23d7846cb47e84fd344543e394e50938540be/lib/_http_server.js#L666

  // If the socket is not writable, there is no reason to try to send data.
  if (socket.writable) {
    socket.write(`HTTP/1.1 ${errorCode} ${errorStatus}\r\nContent-Length: ${body.length}\r\nContent-Type: application/json\r\n\r\n${body}`)
  }
  socket.destroy(err)
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does defaultClientErrorHandler() do?
defaultClientErrorHandler() is a function in the fastify codebase, defined in fastify.js.
Where is defaultClientErrorHandler() defined?
defaultClientErrorHandler() is defined in fastify.js at line 921.

Analyze Your Own Codebase

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

Try Supermodel Free