Errors

Undici exposes a variety of error objects that you can use to enhance your error handling. You can find all the error objects inside the errors key.

import { errors } from 'undici'
ErrorError CodesDescription
UndiciErrorUND_ERRall errors below are extended from UndiciError.
ConnectTimeoutErrorUND_ERR_CONNECT_TIMEOUTsocket is destroyed due to connect timeout.
HeadersTimeoutErrorUND_ERR_HEADERS_TIMEOUTsocket is destroyed due to headers timeout.
HeadersOverflowErrorUND_ERR_HEADERS_OVERFLOWsocket is destroyed due to headers' max size being exceeded.
BodyTimeoutErrorUND_ERR_BODY_TIMEOUTsocket is destroyed due to body timeout.
ResponseStatusCodeErrorUND_ERR_RESPONSE_STATUS_CODEan error is thrown when throwOnError is true for status codes >= 400.
InvalidArgumentErrorUND_ERR_INVALID_ARGpassed an invalid argument.
InvalidReturnValueErrorUND_ERR_INVALID_RETURN_VALUEreturned an invalid value.
RequestAbortedErrorUND_ERR_ABORTEDthe request has been aborted by the user
ClientDestroyedErrorUND_ERR_DESTROYEDtrying to use a destroyed client.
ClientClosedErrorUND_ERR_CLOSEDtrying to use a closed client.
SocketErrorUND_ERR_SOCKETthere is an error with the socket.
NotSupportedErrorUND_ERR_NOT_SUPPORTEDencountered unsupported functionality.
RequestContentLengthMismatchErrorUND_ERR_REQ_CONTENT_LENGTH_MISMATCHrequest body does not match content-length header
ResponseContentLengthMismatchErrorUND_ERR_RES_CONTENT_LENGTH_MISMATCHresponse body does not match content-length header
InformationalErrorUND_ERR_INFOexpected error with reason
ResponseExceededMaxSizeErrorUND_ERR_RES_EXCEEDED_MAX_SIZEresponse body exceed the max size allowed

SocketError

The SocketError has a .socket property which holds socket metadata:

interface SocketInfo {
  localAddress?: string
  localPort?: number
  remoteAddress?: string
  remotePort?: number
  remoteFamily?: string
  timeout?: number
  bytesWritten?: number
  bytesRead?: number
}

Be aware that in some cases the .socket property can be null.