upstash/context7 · error · Context7Error
errorBody.error ?? "http_error"
errorBody.error ?? "http_error"
Error message
errorBody.message || errorBody.error || response.statusText
What it means
Context7 SDK throws a generic Context7Error when an HTTP response is not OK and the body either isn't JSON or lacks message/error fields. The thrown message falls back through errorBody.message, errorBody.error, then the HTTP statusText, and the code defaults to "http_error". It carries status, requestId, rateLimit info, and a retryable flag so callers can react appropriately.
Solutions
- Inspect error.status and the upstream service status page; if status >= 500, retry with backoff (the SDK's retryable flag already hints this).
- Log metadata.requestId and contact Context7 support / check API status when status is 4xx and unexpected.
- Check network/proxy configuration (HTTP(S)_PROXY, firewalls) that may be replacing API responses with gateway error pages.
- If the response is a rate-limit (status 429 or rateLimit metadata present), slow the request rate.
Example fix
// before
const res = await client.request(ctx); // throws opaque http_error
// after
try {
const res = await client.request(ctx);
} catch (e) {
if (e instanceof Context7Error && e.status >= 500 && e.retryable) {
// retry with exponential backoff
} else {
console.error(`request failed: ${e.message} (status=${e.status}, requestId=${e.requestId})`);
}
} Defensive patterns
Strategy: try-catch
Validate before calling
if (!apiKey || typeof apiKey !== 'string') throw new Error('Set CONTEXT7_API_KEY before calling the SDK'); Type guard
function isContext7Error(e: unknown): e is Context7Error {
return e instanceof Context7Error;
} Try / catch
try {
const result = await client.request(ctx);
} catch (e) {
if (e instanceof Context7Error) {
if (e.retryable && (e.status ?? 0) >= 500) return retryWithBackoff();
console.error(`HTTP ${e.status}: ${e.message} (requestId=${e.requestId})`);
} else throw e;
} Prevention
- Wrap SDK calls in a helper that inspects status/retryable and retries 5xx automatically
- Always log metadata.requestId for support requests
- Respect rateLimit metadata to throttle proactively
- Monitor API status pages for gateway incidents
When it happens
Trigger: Any non-2xx response from the Context7 API whose body has no parseable `message` or `error` field: e.g. a bare 502 from a reverse proxy with an HTML/plain-text body, or a JSON body like {"foo":"bar"} with an error status. The code becomes "http_error" whenever errorBody.error is absent.
Common situations: API gateway 5xx pages, Cloudflare block pages with 403/503 status, load-balancer timeouts returning empty bodies, or proxying the SDK through a corporate proxy that intercepts error responses.
Understand the failure class
Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.
Related errors
- Authentication discovery was redirected. Pass the final…
- Failed to fetch
- Failed to fetch user info
- HTTP from /api/auth/mcp
- network_error
AI-assisted analysis of upstash/context7@4416fb855b (2026-09-16).
Data as JSON: /api/errors/277222f6719529aa.
Report an issue: GitHub.
Appendix: source
Thrown at packages/sdk/src/http/response.ts:78
if (rawBody) {
try {
const parsed: unknown = JSON.parse(rawBody);
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
const { error, message } = parsed as Record<string, unknown>;
errorBody = {
error: typeof error === "string" ? error : undefined,
message: typeof message === "string" ? message : undefined,
};
}
} catch (cause) {
if (response.headers.get("content-type")?.includes("application/json")) {
throw jsonParseError(rawBody, metadata, cause, retryable);
}
}
}
throw new Context7Error(errorBody.message || errorBody.error || response.statusText, {
code: errorBody.error ?? "http_error",
status: response.status,
requestId: metadata.requestId,
rateLimit: metadata.rateLimit,
retryable,
});
}
async function parseJson(response: Response, metadata: Context7ResponseMetadata): Promise<unknown> {
const rawBody = await response.text();
try {
return JSON.parse(rawBody);
} catch (cause) {
throw jsonParseError(rawBody, metadata, cause, false);
}
}
function jsonParseError(View on GitHub (pinned to 4416fb855b)