MCPcopy
hub / github.com/fastify/fastify / defaultClientErrorHandler

Function defaultClientErrorHandler

fastify.js:952–990  ·  view source on GitHub ↗
(err, socket)

Source from the content-addressed store, hash-verified

950}
951
952function defaultClientErrorHandler (err, socket) {
953 // In case of a connection reset, the socket has been destroyed and there is nothing that needs to be done.
954 // https://nodejs.org/api/http.html#event-clienterror
955 if (err.code === 'ECONNRESET' || socket.destroyed) {
956 return
957 }
958
959 let body, errorCode, errorStatus, errorLabel
960
961 if (err.code === 'ERR_HTTP_REQUEST_TIMEOUT') {
962 errorCode = '408'
963 errorStatus = http.STATUS_CODES[errorCode]
964 body = `{"error":"${errorStatus}","message":"Client Timeout","statusCode":408}`
965 errorLabel = 'timeout'
966 } else if (err.code === 'HPE_HEADER_OVERFLOW') {
967 errorCode = '431'
968 errorStatus = http.STATUS_CODES[errorCode]
969 body = `{"error":"${errorStatus}","message":"Exceeded maximum allowed HTTP header size","statusCode":431}`
970 errorLabel = 'header_overflow'
971 } else {
972 errorCode = '400'
973 errorStatus = http.STATUS_CODES[errorCode]
974 body = `{"error":"${errorStatus}","message":"Client Error","statusCode":400}`
975 errorLabel = 'error'
976 }
977
978 // Most devs do not know what to do with this error.
979 // In the vast majority of cases, it's a network error and/or some
980 // config issue on the load balancer side.
981 this.log.trace({ err }, `client ${errorLabel}`)
982 // Copying standard node behavior
983 // https://github.com/nodejs/node/blob/6ca23d7846cb47e84fd344543e394e50938540be/lib/_http_server.js#L666
984
985 // If the socket is not writable, there is no reason to try to send data.
986 if (socket.writable) {
987 socket.write(`HTTP/1.1 ${errorCode} ${errorStatus}\r\nContent-Length: ${body.length}\r\nContent-Type: application/json\r\n\r\n${body}`)
988 }
989 socket.destroy(err)
990}
991
992function validateSchemaErrorFormatter (schemaErrorFormatter) {
993 if (typeof schemaErrorFormatter !== 'function') {

Callers

nothing calls this directly

Calls 2

traceMethod · 0.80
writeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…