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

Function defaultClientErrorHandler

fastify.js:952–990  ·  fastify.js::defaultClientErrorHandler
(err, socket)

Source from the content-addressed store, hash-verified

950}
951
952function defaultClientErrorHandler (err, socket) {
953 class="cm">// In case of a connection reset, the socket has been destroyed and there is nothing that needs to be done.
954 class="cm">// https://nodejs.org/api/http.html#event-clienterror
955 if (err.code === class="st">'ECONNRESET' || socket.destroyed) {
956 return
957 }
958
959 let body, errorCode, errorStatus, errorLabel
960
961 if (err.code === class="st">'ERR_HTTP_REQUEST_TIMEOUT') {
962 errorCode = class="st">'408'
963 errorStatus = http.STATUS_CODES[errorCode]
964 body = `{class="st">"error":class="st">"${errorStatus}",class="st">"message":class="st">"Client Timeout",class="st">"statusCode":408}`
965 errorLabel = class="st">'timeout'
966 } else if (err.code === class="st">'HPE_HEADER_OVERFLOW') {
967 errorCode = class="st">'431'
968 errorStatus = http.STATUS_CODES[errorCode]
969 body = `{class="st">"error":class="st">"${errorStatus}",class="st">"message":class="st">"Exceeded maximum allowed HTTP header size",class="st">"statusCode":431}`
970 errorLabel = class="st">'header_overflow'
971 } else {
972 errorCode = class="st">'400'
973 errorStatus = http.STATUS_CODES[errorCode]
974 body = `{class="st">"error":class="st">"${errorStatus}",class="st">"message":class="st">"Client Error",class="st">"statusCode":400}`
975 errorLabel = class="st">'error'
976 }
977
978 class="cm">// Most devs do not know what to do with this error.
979 class="cm">// In the vast majority of cases, it's a network error and/or some
980 class="cm">// config issue on the load balancer side.
981 this.log.trace({ err }, `client ${errorLabel}`)
982 class="cm">// Copying standard node behavior
983 class="cm">// https://github.com/nodejs/node/blob/6ca23d7846cb47e84fd344543e394e50938540be/lib/_http_server.js#L666
984
985 class="cm">// 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 !== class="st">'function') {

Callers

nothing calls this directly

Calls 2

traceMethod · 0.80
writeMethod · 0.80

Tested by

no test coverage detected