UNPKG

@anthropic-ai/sdk

Version:
272 lines • 9.8 kB
import { castToError } from "../internal/errors.mjs"; export var AnthropicError = /* @__PURE__ */ (() => { class AnthropicError extends Error { /** @internal */ constructor(message, options) { super(message, options); Object.defineProperty(this, 'name', { value: new.target._errorName, writable: true, configurable: true }); } } /** @internal */ AnthropicError._errorName = 'AnthropicError'; return AnthropicError; })(); export var APIError = /* @__PURE__ */ (() => { class APIError extends AnthropicError { constructor(status, error, message, headers, type) { super(`${APIError.makeMessage(status, error, message)}`); this.status = status; this.headers = headers; this.requestID = headers?.get('request-id'); this.workspaceID = headers?.get('anthropic-workspace-id'); this.error = error; const body = (error ?? {}); this.apiMessage = body.error?.message ?? null; this.type = type ?? body.error?.type ?? null; const errorCode = body.error?.details?.error_code; this.errorCode = typeof errorCode === 'string' ? errorCode : null; this.details = body.error?.details ?? null; } static makeMessage(status, error, message) { const msg = error?.message ? typeof error.message === 'string' ? error.message : JSON.stringify(error.message) : error ? JSON.stringify(error) : message; if (status && msg) { return `${status} ${msg}`; } if (status) { return `${status} status code (no body)`; } if (msg) { return msg; } return '(no status code or body)'; } static generate(status, errorResponse, message, headers) { if (!status || !headers) { return new APIConnectionError({ message, cause: castToError(errorResponse) }); } const error = errorResponse; const errorClass = STATUS_ERROR_CLASSES.get(status); if (errorClass) { return new errorClass(status, error, message, headers); } if (status >= 500) { return new InternalServerError(status, error, message, headers); } return new APIError(status, error, message, headers); } } /** @internal */ APIError._errorName = 'APIError'; return APIError; })(); export var APIUserAbortError = /* @__PURE__ */ (() => { class APIUserAbortError extends APIError { constructor({ message } = {}) { super(undefined, undefined, message || 'Request was aborted.', undefined); } } /** @internal */ APIUserAbortError._errorName = 'APIUserAbortError'; return APIUserAbortError; })(); export var APIConnectionError = /* @__PURE__ */ (() => { class APIConnectionError extends APIError { constructor({ message, cause }) { super(undefined, undefined, message || 'Connection error.', undefined); // in some environments the 'cause' property is already declared // @ts-ignore if (cause) this.cause = cause; } } /** @internal */ APIConnectionError._errorName = 'APIConnectionError'; return APIConnectionError; })(); export var APIConnectionTimeoutError = /* @__PURE__ */ (() => { class APIConnectionTimeoutError extends APIConnectionError { constructor({ message } = {}) { super({ message: message ?? 'Request timed out.' }); } } /** @internal */ APIConnectionTimeoutError._errorName = 'APIConnectionTimeoutError'; return APIConnectionTimeoutError; })(); /** * An error that opts into the SDK's retry policy: throw it (e.g. from * middleware) to have the attempt retried. * * Note that the request will only be retried when `maxRetries` has not been exhausted. */ export var RetryableError = /* @__PURE__ */ (() => { /** * An error that opts into the SDK's retry policy: throw it (e.g. from * middleware) to have the attempt retried. * * Note that the request will only be retried when `maxRetries` has not been exhausted. */ class RetryableError extends AnthropicError { constructor(message, { cause } = {}) { super(message ?? 'Retryable error.'); // in some environments the 'cause' property is already declared // @ts-ignore if (cause !== undefined) this.cause = cause; } } /** @internal */ RetryableError._errorName = 'RetryableError'; return RetryableError; })(); export var BadRequestError = /* @__PURE__ */ (() => { class BadRequestError extends APIError { } /** @internal */ BadRequestError._errorName = 'BadRequestError'; return BadRequestError; })(); export var AuthenticationError = /* @__PURE__ */ (() => { class AuthenticationError extends APIError { } /** @internal */ AuthenticationError._errorName = 'AuthenticationError'; return AuthenticationError; })(); export var PaymentRequiredError = /* @__PURE__ */ (() => { class PaymentRequiredError extends APIError { } /** @internal */ PaymentRequiredError._errorName = 'PaymentRequiredError'; return PaymentRequiredError; })(); export var PermissionDeniedError = /* @__PURE__ */ (() => { class PermissionDeniedError extends APIError { } /** @internal */ PermissionDeniedError._errorName = 'PermissionDeniedError'; return PermissionDeniedError; })(); export var NotFoundError = /* @__PURE__ */ (() => { class NotFoundError extends APIError { } /** @internal */ NotFoundError._errorName = 'NotFoundError'; return NotFoundError; })(); export var RequestTimeoutError = /* @__PURE__ */ (() => { class RequestTimeoutError extends APIError { } /** @internal */ RequestTimeoutError._errorName = 'RequestTimeoutError'; return RequestTimeoutError; })(); export var ConflictError = /* @__PURE__ */ (() => { class ConflictError extends APIError { } /** @internal */ ConflictError._errorName = 'ConflictError'; return ConflictError; })(); export var PreconditionFailedError = /* @__PURE__ */ (() => { class PreconditionFailedError extends APIError { } /** @internal */ PreconditionFailedError._errorName = 'PreconditionFailedError'; return PreconditionFailedError; })(); export var ContentTooLargeError = /* @__PURE__ */ (() => { class ContentTooLargeError extends APIError { } /** @internal */ ContentTooLargeError._errorName = 'ContentTooLargeError'; return ContentTooLargeError; })(); export var UnprocessableEntityError = /* @__PURE__ */ (() => { class UnprocessableEntityError extends APIError { } /** @internal */ UnprocessableEntityError._errorName = 'UnprocessableEntityError'; return UnprocessableEntityError; })(); export var RateLimitError = /* @__PURE__ */ (() => { class RateLimitError extends APIError { } /** @internal */ RateLimitError._errorName = 'RateLimitError'; return RateLimitError; })(); export var RequestHeaderFieldsTooLargeError = /* @__PURE__ */ (() => { class RequestHeaderFieldsTooLargeError extends APIError { } /** @internal */ RequestHeaderFieldsTooLargeError._errorName = 'RequestHeaderFieldsTooLargeError'; return RequestHeaderFieldsTooLargeError; })(); export var ClientClosedRequestError = /* @__PURE__ */ (() => { class ClientClosedRequestError extends APIError { } /** @internal */ ClientClosedRequestError._errorName = 'ClientClosedRequestError'; return ClientClosedRequestError; })(); export var InternalServerError = /* @__PURE__ */ (() => { class InternalServerError extends APIError { } /** @internal */ InternalServerError._errorName = 'InternalServerError'; return InternalServerError; })(); export var NotImplementedError = /* @__PURE__ */ (() => { class NotImplementedError extends InternalServerError { } /** @internal */ NotImplementedError._errorName = 'NotImplementedError'; return NotImplementedError; })(); export var ServiceUnavailableError = /* @__PURE__ */ (() => { class ServiceUnavailableError extends InternalServerError { } /** @internal */ ServiceUnavailableError._errorName = 'ServiceUnavailableError'; return ServiceUnavailableError; })(); export var GatewayTimeoutError = /* @__PURE__ */ (() => { class GatewayTimeoutError extends InternalServerError { } /** @internal */ GatewayTimeoutError._errorName = 'GatewayTimeoutError'; return GatewayTimeoutError; })(); export var OverloadedError = /* @__PURE__ */ (() => { class OverloadedError extends InternalServerError { } /** @internal */ OverloadedError._errorName = 'OverloadedError'; return OverloadedError; })(); const STATUS_ERROR_CLASSES = /* @__PURE__ */ new Map([ [400, BadRequestError], [401, AuthenticationError], [402, PaymentRequiredError], [403, PermissionDeniedError], [404, NotFoundError], [408, RequestTimeoutError], [409, ConflictError], [412, PreconditionFailedError], [413, ContentTooLargeError], [422, UnprocessableEntityError], [429, RateLimitError], [431, RequestHeaderFieldsTooLargeError], [499, ClientClosedRequestError], [501, NotImplementedError], [503, ServiceUnavailableError], [504, GatewayTimeoutError], [529, OverloadedError], ]); //# sourceMappingURL=error.mjs.map