upstash/context7 · error · TypeError

timeout must be a positive number or false

Error message

timeout must be a positive number or false

What it means

validateTimeout enforces that a timeout option is either the literal false (disabling timeouts) or a finite positive number of milliseconds. Anything else — NaN, 0, negative numbers, Infinity — throws a TypeError before any request is made.

Solutions

  1. Pass a positive finite number of milliseconds, e.g. timeout: 5000
  2. Pass timeout: false to explicitly disable the timeout (0 does NOT disable it)
  3. Parse env/config values with Number() and check Number.isFinite before use
  4. Fallback to a default when the configured value is absent or invalid

Example fix

// before
const timeout = Number(process.env.TIMEOUT); // '' -> 0
// after
const raw = Number(process.env.TIMEOUT);
const timeout = Number.isFinite(raw) && raw > 0 ? raw : false;
Defensive patterns

Strategy: validation

Validate before calling

function resolveTimeout(v) {
  if (v === false) return false;
  const n = Number(v);
  return Number.isFinite(n) && n > 0 ? n : 30000; // safe default
}

Type guard

function isValidTimeout(v: unknown): v is number | false {
  return v === false || (typeof v === 'number' && Number.isFinite(v) && v > 0);
}

Try / catch

try {
  const client = new Context7Client({ timeout });
} catch (e) {
  if (e instanceof TypeError && e.message.includes('timeout must be a positive number')) {
    console.error('timeout must be positive milliseconds or false to disable');
  } else { throw e; }
}

Prevention

When it happens

Trigger: Passing timeout: 0 expecting 'no timeout', a negative number, Infinity, NaN (often from Number(unparseable)), or a non-numeric value via a config parsed from env/JSON, to createAbortState or an HTTP client constructor.

Common situations: Reading timeout from an env var or CLI flag without parsing to a number; JSON config containing null or a string like '5000'; developers using 0 to mean 'infinite' (the library uses false for that); JS arithmetic producing NaN.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of upstash/context7@4416fb855b (2026-09-16). Data as JSON: /api/errors/c1d750953fb09342. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/src/http/abort.ts:11

import { Context7Error } from "@error";

export type AbortState = {
  signal?: AbortSignal;
  timedOut: () => boolean;
  cleanup: () => void;
};

export function validateTimeout(timeout: number | false): void {
  if (timeout !== false && (!Number.isFinite(timeout) || timeout <= 0)) {
    throw new TypeError("timeout must be a positive number or false");
  }
}

export function resolveSignal(signal?: AbortSignal | (() => AbortSignal)): AbortSignal | undefined {
  return typeof signal === "function" ? signal() : signal;
}

export function createAbortState(
  signals: Array<AbortSignal | undefined>,
  timeout: number | false
): AbortState {
  validateTimeout(timeout);

  const activeSignals = [
    ...new Set(signals.filter((signal): signal is AbortSignal => signal !== undefined)),
  ];
  if (timeout === false && activeSignals.length === 0) {
    return { timedOut: () => false, cleanup: () => undefined };

View on GitHub (pinned to 4416fb855b)