{"record":{"id":"277222f6719529aa","repo":"upstash/context7","slug":"errorbody-error-http-error","errorCode":"errorBody.error ?? \"http_error\"","errorMessage":"errorBody.message || errorBody.error || response.statusText","messagePattern":"errorBody\\.message \\|\\| errorBody\\.error \\|\\| response\\.statusText","errorType":"exception","errorClass":"Context7Error","httpStatus":null,"severity":"error","filePath":"packages/sdk/src/http/response.ts","lineNumber":78,"sourceCode":"\n  if (rawBody) {\n    try {\n      const parsed: unknown = JSON.parse(rawBody);\n      if (typeof parsed === \"object\" && parsed !== null && !Array.isArray(parsed)) {\n        const { error, message } = parsed as Record<string, unknown>;\n        errorBody = {\n          error: typeof error === \"string\" ? error : undefined,\n          message: typeof message === \"string\" ? message : undefined,\n        };\n      }\n    } catch (cause) {\n      if (response.headers.get(\"content-type\")?.includes(\"application/json\")) {\n        throw jsonParseError(rawBody, metadata, cause, retryable);\n      }\n    }\n  }\n\n  throw new Context7Error(errorBody.message || errorBody.error || response.statusText, {\n    code: errorBody.error ?? \"http_error\",\n    status: response.status,\n    requestId: metadata.requestId,\n    rateLimit: metadata.rateLimit,\n    retryable,\n  });\n}\n\nasync function parseJson(response: Response, metadata: Context7ResponseMetadata): Promise<unknown> {\n  const rawBody = await response.text();\n  try {\n    return JSON.parse(rawBody);\n  } catch (cause) {\n    throw jsonParseError(rawBody, metadata, cause, false);\n  }\n}\n\nfunction jsonParseError(","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/upstash/context7/blob/4416fb855b8f752be735e34f943b5d0762701aad/packages/sdk/src/http/response.ts#L60-L96","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst res = await client.request(ctx); // throws opaque http_error\n// after\ntry {\n  const res = await client.request(ctx);\n} catch (e) {\n  if (e instanceof Context7Error && e.status >= 500 && e.retryable) {\n    // retry with exponential backoff\n  } else {\n    console.error(`request failed: ${e.message} (status=${e.status}, requestId=${e.requestId})`);\n  }\n}","handlingStrategy":"try-catch","validationCode":"if (!apiKey || typeof apiKey !== 'string') throw new Error('Set CONTEXT7_API_KEY before calling the SDK');","typeGuard":"function isContext7Error(e: unknown): e is Context7Error {\n  return e instanceof Context7Error;\n}","tryCatchPattern":"try {\n  const result = await client.request(ctx);\n} catch (e) {\n  if (e instanceof Context7Error) {\n    if (e.retryable && (e.status ?? 0) >= 500) return retryWithBackoff();\n    console.error(`HTTP ${e.status}: ${e.message} (requestId=${e.requestId})`);\n  } else throw e;\n}","preventionTips":["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"],"tags":["http","network","fallback-message"],"backgroundTag":"http-error-response","analyzedSha":"4416fb855b8f752be735e34f943b5d0762701aad","analyzedAt":"2026-09-16T20:28:07.148Z","contentChangedAt":"2026-09-16T20:28:07.148Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}