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

Function defaultClientErrorHandler

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

traceMethod · 0.80
writeMethod · 0.80

Tested by

no test coverage detected