superagent
semver
>=3.7.0postconditions14functions9last verified2026-04-16coverage score100%Postconditions — what we check
- get · network-error-handlingerrorWhennetwork failure, DNS error, timeout, connection refused, HTTP 4xx/5xx errorsThrows
Error with status, response, timeout fields (Promise rejection)Required handlingUse try-catch (async/await) or .catch() (promises) or .end() callbackcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - get · timeout-error-identifiablewarningWhenrequest exceeds deadline (.timeout(ms)) or response timeout (.timeout({response:ms}))Throws
Error with err.timeout=<number> and err.code='ETIME' (deadline) or 'ETIMEDOUT' (response timeout)Required handlingCheck err.timeout in catch block to distinguish timeout from network errors; retry with backoff if appropriatecostlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[2] - get · max-response-size-exceedederrorWhenresponse body exceeds maxResponseSize limit (default 200MB for buffered responses)Throws
Error with code='ETOOLARGE' and message='Maximum response size reached'Required handlingHandle in catch block; check err.code === 'ETOOLARGE' for specific handling; use streaming .pipe() for large responses instead of bufferingcostmediumin prodimmediate exceptionusers seelost datavisibilitysilentSources[3] - post · network-error-handlingerrorWhennetwork failure, DNS error, timeout, connection refused, HTTP 4xx/5xx errorsThrows
Error with status, response, timeout fields (Promise rejection)Required handlingUse try-catch (async/await) or .catch() (promises) or .end() callbackcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - post · timeout-error-identifiablewarningWhenrequest exceeds deadline or response timeoutThrows
Error with err.timeout=<number> and err.code='ETIME' (deadline) or 'ETIMEDOUT' (response timeout)Required handlingCheck err.timeout in catch to distinguish timeout from auth/rate-limit errors; POST requests are not idempotent so do not auto-retry without deduplicationcostlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[2] - put · network-error-handlingerrorWhennetwork failure, DNS error, timeout, connection refused, HTTP 4xx/5xx errorsThrows
Error with status, response, timeout fields (Promise rejection)Required handlingUse try-catch (async/await) or .catch() (promises) or .end() callbackcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - patch · network-error-handlingerrorWhennetwork failure, DNS error, timeout, connection refused, HTTP 4xx/5xx errorsThrows
Error with status, response, timeout fields (Promise rejection)Required handlingUse try-catch (async/await) or .catch() (promises) or .end() callbackcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - delete · network-error-handlingerrorWhennetwork failure, DNS error, timeout, connection refused, HTTP 4xx/5xx errorsThrows
Error with status, response, timeout fields (Promise rejection)Required handlingUse try-catch (async/await) or .catch() (promises) or .end() callbackcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - del · network-error-handlingerrorWhennetwork failure, DNS error, timeout, connection refused, HTTP 4xx/5xx errorsThrows
Error with status, response, timeout fields (Promise rejection)Required handlingUse try-catch (async/await) or .catch() (promises) or .end() callbackcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - head · network-error-handlingerrorWhennetwork failure, DNS error, timeout, connection refused, HTTP 4xx/5xx errorsThrows
Error with status, response, timeout fields (Promise rejection)Required handlingUse try-catch (async/await) or .catch() (promises) or .end() callbackcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - pipe · pipe-errors-not-promise-rejectionserrorWhenAny error during a piped request: network failure, DNS resolution error, connection refused, HTTP 4xx/5xx response, or timeout. Unlike awaited superagent calls, piped requests bypass the Promise chain entirely — .pipe() returns the destination stream immediately, not a Promise.Throws
Does NOT throw or reject a Promise. Errors are emitted as 'error' events: (1) Network/HTTP errors: emitted on the Request object via req.emit('error', err) where err.status is set for HTTP errors, absent for network/DNS errors. (2) Decompression errors (gzip/deflate responses): emitted on the destination stream via stream.emit('error', err). err.code='Z_BUF_ERROR' (truncated gzip) is silently swallowed by superagent; other zlib errors propagate to dest stream. If no 'error' listener is registered, Node.js throws an uncaught exception and may crash the process.Required handlingMUST register an 'error' event listener on the Request object before calling pipe(). Try-catch and async/await provide zero protection — errors bypass them silently, leaving the destination stream in an incomplete state with no indication of failure. Correct pattern: const req = superagent.get(url); req.on('error', handler); req.pipe(dest); Also register 'error' on the destination stream for decompression errors: dest.on('error', (err) => { /* handle decompression errors */ });costhighin prodsilent failureusers seelost datavisibilitysilent - pipe · pipe-cannot-be-mixed-with-promiseerrorWhenCalling .pipe() after .then()/.catch()/await on the same request, or attempting to pipe the Response object (res.pipe()) received in an .end() callback. Common mistake: const res = await superagent.get(url); res.pipe(stream) — pipe() belongs on the Request, not the Response, and await has already consumed the response via end().Throws
Synchronous Error with message "end() has already been called, so it's too late to start piping" when pipe() is called on the Response object after end() has run. If pipe() is called on the Request after await (same request), behavior is undefined and data may be silently lost without any error.Required handlingUse EITHER streaming OR promise/callback — never both on the same request. Streaming: const req = superagent.get(url); req.on('error', h); req.pipe(stream); Promise: const res = await superagent.get(url); process(res.body); If a response must be both processed and streamed, buffer first then write manually.costmediumin prodimmediate exceptionusers seelost datavisibilityvisibleSources[4] - agent · agent-request-network-errorerrorWhennetwork failure, DNS error, connection refused, or HTTP 4xx/5xx on agent.get/post/put/patch/delete/head()Throws
Error with err.status (HTTP errors) or no err.status (network errors), plus err.response for HTTP errorsRequired handlingWrap agent HTTP method calls in try-catch; agent persists cookies so session errors (401, 403) may indicate expired auth that must be re-establishedcostmediumin prodimmediate exceptionusers seeservice unavailablevisibilitysilentSources[6] - agent · agent-session-auth-failureerrorWhen401 Unauthorized or 403 Forbidden response when agent session/cookie has expiredThrows
Error with err.status=401 or err.status=403 and err.response containing the response bodyRequired handlingCatch 401/403 specifically and re-authenticate: check err.status === 401 to detect session expiry. Agents carry cookies across requests — a 401 mid-session means the cookie/token expired and must be refreshed before retrying. Do not silently swallow these errors or the agent will continue sending expired credentials.costmediumin prodimmediate exceptionusers seeauthentication failurevisibilitysilent
Sources
Every postcondition cites at least one of these. Numbered to match the footnotes above.
- [1]forwardemail.github.io/superagenthttps://forwardemail.github.io/superagent/
- [2]forwardemail.github.io/superagenthttps://forwardemail.github.io/superagent/#timeouts
- [3]forwardemail.github.io/superagenthttps://forwardemail.github.io/superagent/#response-body
- [4]forwardemail.github.io/superagenthttps://forwardemail.github.io/superagent/#piping
- [5]forwardemail.github.io/superagenthttps://forwardemail.github.io/superagent/#error-handling
- [6]forwardemail.github.io/superagenthttps://forwardemail.github.io/superagent/#agents-for-global-state
Need a different package?
Request a profile