Context7 Platform -- Up-to-date docs for AI agents
import type { RateLimitMetadata } from "../http/types";
export type Context7ErrorOptions = {
/** Machine-readable error code returned by Context7 or generated by the SDK. */
code?: string;
/** HTTP status code, when the request reached the Context7 API. */
status?: number;
/** Request identifier returned by the Context7 API. */
requestId?: string;
/** Rate-limit state returned with the failed request. */
rateLimit?: RateLimitMetadata;
/** Whether retrying the request may succeed. */
retryable?: boolean;
/** Original error that caused this error. */
cause?: unknown;
};
export class Context7Error extends Error {
public readonly code?: string;
public readonly status?: number;
public readonly requestId?: string;
public readonly rateLimit?: RateLimitMetadata;
public readonly retryable: boolean;
public override readonly cause?: unknown;
constructor(message: string, options: Context7ErrorOptions = {}) {
super(message);
this.name = "Context7Error";
this.code = options.code;
this.status = options.status;
this.requestId = options.requestId;
this.rateLimit = options.rateLimit;
this.retryable = options.retryable ?? false;
this.cause = options.cause;
}
}
/** Raised when a configured API URL is not a valid HTTP(S) URL. */
export class Context7UrlError extends Context7Error {
constructor(url: string) {
super(
`Context7 client was passed an invalid URL. You should pass a URL starting with http:// or https://. Received: "${url}".`,
{ code: "invalid_url" }
);
this.name = "Context7UrlError";
}
}
/** Raised when a response advertised as JSON cannot be parsed. */
export class Context7JSONParseError extends Context7Error {
constructor(body: string, options: Context7ErrorOptions = {}) {
const truncatedBody = body.length > 200 ? `${body.slice(0, 200)}...` : body;
super(`Unable to parse response body: ${truncatedBody}`, {
...options,
code: options.code ?? "invalid_json_response",
});
this.name = "Context7JSONParseError";
}
}