From bf6723bec6e5be551d00b7acd9d8adf8be998763 Mon Sep 17 00:00:00 2001 From: theamodhshetty Date: Tue, 3 Mar 2026 09:16:23 +0530 Subject: [PATCH] docs(errors): explain timeout error codes and fetch configuration --- docs/docs/api/Errors.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/docs/api/Errors.md b/docs/docs/api/Errors.md index c00729009f9..7a79c5efe0b 100644 --- a/docs/docs/api/Errors.md +++ b/docs/docs/api/Errors.md @@ -32,6 +32,37 @@ if (err.code === 'UND_ERR_CONNECT_TIMEOUT') { } ``` +## Timeout-related errors + +The timeout errors below are controlled by dispatcher/client options: + +| Error code | When it is thrown | Related option | +| ---------- | ----------------- | -------------- | +| `UND_ERR_CONNECT_TIMEOUT` | A connection could not be established before the connection timeout elapsed. | [`connect.timeout`](Client.md) | +| `UND_ERR_HEADERS_TIMEOUT` | The full response headers were not received before the headers timeout elapsed. | [`headersTimeout`](Client.md) | +| `UND_ERR_BODY_TIMEOUT` | While reading the response body, no new data arrived before the body timeout elapsed. | [`bodyTimeout`](Client.md) | +| `UND_ERR_SOCKET` | A generic socket-level failure happened. | Inspect `error.socket` for connection details. | + +### Configuring timeouts for `fetch` + +If you are using `fetch` (including Node.js built-in `fetch`), configure timeouts +through the global dispatcher: + +```js +import { Agent, setGlobalDispatcher, fetch } from 'undici' + +setGlobalDispatcher(new Agent({ + connect: { timeout: 10_000 }, // connection phase + headersTimeout: 15_000, // wait for response headers + bodyTimeout: 30_000 // wait between response body chunks +})) + +await fetch('https://example.com/data') +``` + +You can inspect timeout error codes from `error.code` or (for wrapped fetch +errors) from `error.cause?.code`. + ## Class: `UndiciError`