On this page

M

Errors

History
Source Code: lib/core/errors.js
Stability: 2Stable

undici raises typed error objects so that failures can be distinguished programmatically. Every error class is exposed through the errors namespace of the package:

import { errors } from 'undici'

if (err instanceof errors.ConnectTimeoutError) {
  // handle connect timeout
}

All errors, except HTTPParserError, extend UndiciError. Each error carries a stable code string (for example UND_ERR_CONNECT_TIMEOUT) and a name.

Because the bundled (global) dispatcher may come from a different undici version than the one you import directly, prefer matching on error.code rather than instanceof errors.UndiciError:

if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
  // handle connect timeout
}
class UndiciError extends Error

The base class for all undici errors. It is reliable with instanceof even when the error originates from a different undici version, because the check is based on a well-known symbol rather than the prototype chain.

Attributes
Always  'UndiciError' .
Always  'UND_ERR' .
class ConnectTimeoutError extends UndiciError

The socket was destroyed because establishing the connection exceeded the connectTimeout option.

Attributes
Always  'ConnectTimeoutError' .
Always  'UND_ERR_CONNECT_TIMEOUT' .

When autoSelectFamily is enabled and every attempted address times out, Node.js raises an AggregateError. undici normalizes these multi-address timeouts into a ConnectTimeoutError so that the error shape is the same regardless of whether Node.js's per-address timer or undici's connectTimeout wins the race. The original AggregateError is preserved on error.cause.

class HeadersTimeoutError extends UndiciError

The socket was destroyed because receiving the response headers exceeded the headersTimeout option.

Attributes
Always  'HeadersTimeoutError' .
Always  'UND_ERR_HEADERS_TIMEOUT' .
class HeadersOverflowError extends UndiciError

The socket was destroyed because the response headers exceeded the maximum allowed size.

Attributes
Always  'HeadersOverflowError' .
Always  'UND_ERR_HEADERS_OVERFLOW' .
class BodyTimeoutError extends UndiciError

The socket was destroyed because reading the response body exceeded the bodyTimeout option.

Attributes
Always  'BodyTimeoutError' .
Always  'UND_ERR_BODY_TIMEOUT' .
class InvalidArgumentError extends UndiciError

An invalid argument was passed to an undici API.

Attributes
Always  'InvalidArgumentError' .
Always  'UND_ERR_INVALID_ARG' .
class InvalidReturnValueError extends UndiciError

A user-supplied callback or interceptor returned an invalid value.

Attributes
Always  'InvalidReturnValueError' .
Always  'UND_ERR_INVALID_RETURN_VALUE' .
class AbortError extends UndiciError

The operation was aborted. This is the base class of RequestAbortedError.

Attributes
Always  'AbortError' .
Always  'UND_ERR_ABORT' .
class RequestAbortedError extends AbortError

The request was aborted by the user, typically through an AbortSignal.

Attributes
Always  'AbortError' .
Always  'UND_ERR_ABORTED' .
class InformationalError extends UndiciError

An expected error carrying a reason, used internally to surface a cause for a failed or retried request (for example as the cause of another error).

Attributes
Always  'InformationalError' .
Always  'UND_ERR_INFO' .
class RequestContentLengthMismatchError extends UndiciError

The length of the request body did not match the content-length header.

Attributes
Always  'RequestContentLengthMismatchError' .
Always  'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH' .
class ResponseContentLengthMismatchError extends UndiciError

The length of the response body did not match the content-length header.

Attributes
Always  'ResponseContentLengthMismatchError' .
Always  'UND_ERR_RES_CONTENT_LENGTH_MISMATCH' .
class ClientDestroyedError extends UndiciError

An operation was attempted on a client that has already been destroyed.

Attributes
Always  'ClientDestroyedError' .
Always  'UND_ERR_DESTROYED' .
class ClientClosedError extends UndiciError

An operation was attempted on a client that has already been closed.

Attributes
Always  'ClientClosedError' .
Always  'UND_ERR_CLOSED' .
class SocketError extends UndiciError

An error occurred on the underlying socket.

Attributes
Always  'SocketError' .
Always  'UND_ERR_SOCKET' .
Metadata about the socket at the time of the error, or  null when no socket information is available.

The socket property, when present, holds the following fields:

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

Unsupported functionality was requested.

Attributes
Always  'NotSupportedError' .
Always  'UND_ERR_NOT_SUPPORTED' .
class BalancedPoolMissingUpstreamError extends UndiciError

No upstream has been added to the BalancedPool.

Attributes
Always  'MissingUpstreamError' .
Always  'UND_ERR_BPL_MISSING_UPSTREAM' .
C

HTTPParserError

History
class HTTPParserError extends Error

An error occurred while parsing the HTTP response. Unlike the other error classes, HTTPParserError extends <Error> directly rather than UndiciError.

Attributes
Always  'HTTPParserError' .
An  HPE_ -prefixed parser error code, or undefined when no code was provided.
The raw data associated with the parser failure, or  undefined when none was provided.
class ResponseExceededMaxSizeError extends UndiciError

The response body exceeded the maximum allowed size.

Attributes
Always  'ResponseExceededMaxSizeError' .
Always  'UND_ERR_RES_EXCEEDED_MAX_SIZE' .
class RequestRetryError extends UndiciError

A request failed and could not be retried, for example because the body is stateful (a stream or AsyncIterable) and cannot be replayed.

Attributes
Always  'RequestRetryError' .
Always  'UND_ERR_REQ_RETRY' .
statusCode:<number>
The HTTP status code of the failed response.
Retry metadata.
count:<number>
The number of retry attempts that were made.
headers:<Record>
< <string> , <string> | <string> []> The response headers.
new RequestRetryError(message, code, options): void
Attributes
message:<string>
The error message.
The HTTP status code of the failed response.
options:<Object>
headers:<Object> | <string>
[] | <null> The response headers. (optional)
Retry metadata. (optional)
class ResponseError extends UndiciError

The response returned an error status code. This is raised, for example, when the throwOnError option is enabled.

Attributes
Always  'ResponseError' .
Always  'UND_ERR_RESPONSE' .
statusCode:<number>
The HTTP status code of the response.
The response body.
headers:<Object> | <string>
[] | <null> The response headers.
new ResponseError(message, code, options): void
Attributes
message:<string>
The error message.
The HTTP status code of the response.
options:<Object>
headers:<Object> | <string>
[] | <null> The response headers. (optional)
The response body. (optional)
class SecureProxyConnectionError extends UndiciError

A TLS connection to a proxy failed.

Attributes
Always  'SecureProxyConnectionError' .
Always  'UND_ERR_PRX_TLS' .
cause:<Error>
The underlying error that caused the proxy connection to fail.
new SecureProxyConnectionError(cause, message?, options?): void
Attributes
cause:<Error>
The underlying error. (optional)
message:<string>
The error message. (optional)
options:<Object>
Additional  Error options merged with cause . (optional)
class MaxOriginsReachedError extends UndiciError

The maximum number of allowed origins has been reached.

Attributes
Always  'MaxOriginsReachedError' .
Always  'UND_ERR_MAX_ORIGINS_REACHED' .
C

Socks5ProxyError

History
class Socks5ProxyError extends UndiciError

An error occurred during SOCKS5 proxy negotiation.

Attributes
Always  'Socks5ProxyError' .
code?:<string>
The error code.  Default: 'UND_ERR_SOCKS5' .
new Socks5ProxyError(message?, code?): void
Attributes
message:<string>
The error message. (optional)
code?:<string>
The error code.  Default: 'UND_ERR_SOCKS5' . (optional)
C

MessageSizeExceededError

History
class MessageSizeExceededError extends UndiciError

A decompressed WebSocket message exceeded the maximum allowed size.

Attributes
Always  'MessageSizeExceededError' .
Always  'UND_ERR_WS_MESSAGE_SIZE_EXCEEDED' .