UNPKG

@coinbase/cdp-sdk

Version:

SDK for interacting with the Coinbase Developer Platform Wallet API

140 lines 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NetworkError = exports.UnknownError = exports.UnknownApiError = exports.APIError = void 0; exports.isOpenAPIError = isOpenAPIError; const constants_js_1 = require("../constants.js"); /** * Extended API error that encompasses both OpenAPI errors and other API-related errors */ class APIError extends Error { statusCode; errorType; errorMessage; correlationId; errorLink; /** * Constructor for the APIError class * * @param statusCode - The HTTP status code * @param errorType - The type of error * @param errorMessage - The error message * @param correlationId - The correlation ID * @param errorLink - URL to documentation about this error * @param cause - The cause of the error */ constructor(statusCode, errorType, errorMessage, correlationId, errorLink, cause) { super(errorMessage, { cause }); this.name = "APIError"; this.statusCode = statusCode; this.errorType = errorType; this.errorMessage = errorMessage; // Only set correlationId if it's defined if (correlationId !== undefined) { this.correlationId = correlationId; } // Only set errorLink if it's defined if (errorLink !== undefined) { this.errorLink = errorLink; } } /** * Convert the error to a JSON object, excluding undefined properties * * @returns The error as a JSON object */ toJSON() { return { name: this.name, statusCode: this.statusCode, errorType: this.errorType, errorMessage: this.errorMessage, ...(this.correlationId && { correlationId: this.correlationId }), ...(this.errorLink && { errorLink: this.errorLink }), }; } } exports.APIError = APIError; /** * Error thrown when an Axios request is made but no response is received */ class UnknownApiError extends APIError { /** * Constructor for the UnknownApiError class * * @param errorType - The type of error * @param errorMessage - The error message * @param cause - The cause of the error */ constructor(errorType, errorMessage, cause) { super(0, errorType, errorMessage, undefined, undefined, cause); this.name = "UnknownApiError"; } } exports.UnknownApiError = UnknownApiError; /** * Error thrown when an error is not known */ class UnknownError extends Error { /** * Constructor for the UnknownError class * * @param message - The error message * @param cause - The cause of the error */ constructor(message, cause) { super(message, { cause }); this.name = "UnknownError"; } } exports.UnknownError = UnknownError; /** * Error thrown when a network-level failure occurs before reaching the CDP service * This includes gateway errors, IP blocklist rejections, DNS failures, etc. */ class NetworkError extends APIError { networkDetails; /** * Constructor for the NetworkError class * * @param errorType - The type of network error * @param errorMessage - The error message * @param networkDetails - Additional network error details * @param networkDetails.code - The error code * @param networkDetails.message - The error message * @param networkDetails.retryable - Whether the error is retryable * @param cause - The cause of the error */ constructor(errorType, errorMessage, networkDetails, cause) { super(0, // Status code 0 indicates no response was received errorType, errorMessage, undefined, `${constants_js_1.ERROR_DOCS_PAGE_URL}#network-errors`, cause); this.name = "NetworkError"; this.networkDetails = networkDetails; } /** * Convert the error to a JSON object, including network details * * @returns The error as a JSON object */ toJSON() { return { ...super.toJSON(), ...(this.networkDetails && { networkDetails: this.networkDetails }), }; } } exports.NetworkError = NetworkError; /** * Type guard to check if an object is an OpenAPIError * * @param obj - The object to check * @returns True if the object is an OpenAPIError */ function isOpenAPIError(obj) { return (obj !== null && typeof obj === "object" && "errorType" in obj && typeof obj.errorType === "string" && "errorMessage" in obj && typeof obj.errorMessage === "string"); } //# sourceMappingURL=errors.js.map