UNPKG

amazon-seller-mcp

Version:

Model Context Protocol (MCP) client for Amazon Selling Partner API

334 lines (333 loc) 9.41 kB
/** * Error handling utilities for Amazon Seller MCP Client * * This file contains error classes, error translation functions, and error recovery strategies * for handling errors from the Amazon Selling Partner API and translating them to MCP errors. */ import { ApiError } from '../types/api.js'; import { ErrorDetails } from '../types/common.js'; /** * Error recovery context */ interface ErrorRecoveryContext { operation?: () => Promise<unknown>; retryCount?: number; maxRetries?: number; [key: string]: unknown; } /** * Constructor type for Amazon Seller MCP Error classes */ type AmazonSellerMcpErrorConstructor = new (message: string, details?: ErrorDetails, cause?: Error) => AmazonSellerMcpError; /** * MCP error details */ interface McpErrorDetails { code: string; message: string; details?: ErrorDetails; } /** * Base error class for Amazon Seller MCP Client */ export declare class AmazonSellerMcpError extends Error { /** * Error code */ code: string; /** * Error details */ details?: ErrorDetails; /** * Original error */ cause?: Error; /** * Create a new Amazon Seller MCP error * * @param message Error message * @param code Error code * @param details Error details * @param cause Original error */ constructor(message: string, code: string, details?: ErrorDetails, cause?: Error); } /** * Authentication error */ export declare class AuthenticationError extends AmazonSellerMcpError { constructor(message: string, details?: ErrorDetails, cause?: Error); } /** * Authorization error */ export declare class AuthorizationError extends AmazonSellerMcpError { constructor(message: string, details?: ErrorDetails, cause?: Error); } /** * Validation error */ export declare class ValidationError extends AmazonSellerMcpError { constructor(message: string, details?: ErrorDetails, cause?: Error); } /** * Resource not found error */ export declare class ResourceNotFoundError extends AmazonSellerMcpError { constructor(message: string, details?: ErrorDetails, cause?: Error); } /** * Rate limit exceeded error */ export declare class RateLimitExceededError extends AmazonSellerMcpError { /** * Time to wait before retrying */ retryAfterMs: number; constructor(message: string, retryAfterMs: number, details?: ErrorDetails, cause?: Error); } /** * Server error */ export declare class ServerError extends AmazonSellerMcpError { constructor(message: string, details?: ErrorDetails, cause?: Error); } /** * Network error */ export declare class NetworkError extends AmazonSellerMcpError { constructor(message: string, details?: ErrorDetails, cause?: Error); } /** * Throttling error */ export declare class ThrottlingError extends AmazonSellerMcpError { /** * Time to wait before retrying */ retryAfterMs: number; constructor(message: string, retryAfterMs: number, details?: ErrorDetails, cause?: Error); } /** * Marketplace error */ export declare class MarketplaceError extends AmazonSellerMcpError { constructor(message: string, details?: ErrorDetails, cause?: Error); } /** * Translate an API error to a specific Amazon Seller MCP error * * @param error API error * @returns Amazon Seller MCP error */ export declare function translateApiError(error: ApiError): AmazonSellerMcpError; /** * Translate an Amazon Seller MCP error to an MCP error response * * @param error Amazon Seller MCP error * @returns MCP error response */ export declare function translateToMcpErrorResponse(error: AmazonSellerMcpError | Error): { content: Array<{ type: 'text'; text: string; } | { type: 'resource_link'; uri: string; name: string; mimeType?: string; description?: string; }>; isError: boolean; errorDetails?: McpErrorDetails; }; /** * Error recovery strategy */ export interface ErrorRecoveryStrategy { /** * Whether the error can be recovered from * * @param error Error to check * @returns Whether the error can be recovered from */ canRecover: (error: AmazonSellerMcpError | Error) => boolean; /** * Recover from the error * * @param error Error to recover from * @param context Recovery context * @returns Promise resolving to the recovery result */ recover: <T>(error: AmazonSellerMcpError | Error, context: ErrorRecoveryContext) => Promise<T>; } /** * Retry recovery strategy */ export declare class RetryRecoveryStrategy implements ErrorRecoveryStrategy { /** * Maximum number of retries */ private maxRetries; /** * Base delay in milliseconds */ private baseDelayMs; /** * Maximum delay in milliseconds */ private maxDelayMs; /** * Create a new retry recovery strategy * * @param maxRetries Maximum number of retries * @param baseDelayMs Base delay in milliseconds * @param maxDelayMs Maximum delay in milliseconds */ constructor(maxRetries?: number, baseDelayMs?: number, maxDelayMs?: number); /** * Whether the error can be recovered from * * @param error Error to check * @returns Whether the error can be recovered from */ canRecover(error: AmazonSellerMcpError | Error): boolean; /** * Recover from the error * * @param error Error to recover from * @param context Recovery context * @returns Promise resolving to the recovery result */ recover<T>(error: AmazonSellerMcpError | Error, context: ErrorRecoveryContext): Promise<T>; } /** * Fallback recovery strategy */ export declare class FallbackRecoveryStrategy implements ErrorRecoveryStrategy { /** * Fallback function */ private fallbackFn; /** * Error types that can be recovered from */ private recoverableErrors; /** * Create a new fallback recovery strategy * * @param fallbackFn Fallback function * @param recoverableErrors Error types that can be recovered from */ constructor(fallbackFn: (error: AmazonSellerMcpError | Error, context: ErrorRecoveryContext) => Promise<unknown>, recoverableErrors?: Array<AmazonSellerMcpErrorConstructor>); /** * Whether the error can be recovered from * * @param error Error to check * @returns Whether the error can be recovered from */ canRecover(error: AmazonSellerMcpError | Error): boolean; /** * Recover from the error * * @param error Error to recover from * @param context Recovery context * @returns Promise resolving to the recovery result */ recover<T>(error: AmazonSellerMcpError | Error, context: ErrorRecoveryContext): Promise<T>; } /** * Circuit breaker recovery strategy */ export declare class CircuitBreakerRecoveryStrategy implements ErrorRecoveryStrategy { /** * Circuit breaker state */ private state; /** * Failure count */ private failureCount; /** * Last failure time */ private lastFailureTime; /** * Failure threshold */ private failureThreshold; /** * Reset timeout in milliseconds */ private resetTimeoutMs; /** * Error types that can trip the circuit breaker */ private tripErrors; /** * Create a new circuit breaker recovery strategy * * @param failureThreshold Failure threshold * @param resetTimeoutMs Reset timeout in milliseconds * @param tripErrors Error types that can trip the circuit breaker */ constructor(failureThreshold?: number, resetTimeoutMs?: number, tripErrors?: Array<AmazonSellerMcpErrorConstructor>); /** * Whether the error can be recovered from * * @param error Error to check * @returns Whether the error can be recovered from */ canRecover(error: AmazonSellerMcpError | Error): boolean; /** * Recover from the error * * @param error Error to recover from * @param context Recovery context * @returns Promise resolving to the recovery result */ recover<T>(error: AmazonSellerMcpError | Error, context: ErrorRecoveryContext): Promise<T>; /** * Update the circuit breaker state * * @param error Error that occurred */ private updateState; } /** * Error recovery manager */ export declare class ErrorRecoveryManager { /** * Recovery strategies */ private strategies; /** * Create a new error recovery manager * * @param strategies Recovery strategies */ constructor(strategies?: ErrorRecoveryStrategy[]); /** * Add a recovery strategy * * @param strategy Recovery strategy */ addStrategy(strategy: ErrorRecoveryStrategy): void; /** * Execute an operation with error recovery * * @param operation Operation to execute * @param context Recovery context * @returns Promise resolving to the operation result */ executeWithRecovery<T>(operation: () => Promise<T>, context?: ErrorRecoveryContext): Promise<T>; } /** * Create a default error recovery manager * * @returns Default error recovery manager */ export declare function createDefaultErrorRecoveryManager(): ErrorRecoveryManager; export {};