UNPKG

perplexity-mcp-server

Version:

A Perplexity API Model Context Protocol (MCP) server that unlocks Perplexity's search-augmented AI capabilities for LLM agents. Features robust error handling, secure input validation, and transparent reasoning with the showThinking parameter. Built with

91 lines (90 loc) 3.31 kB
import { BaseErrorCode } from '../types-global/errors.js'; /** * Generic error context interface */ export interface ErrorContext { /** Unique request or operation identifier */ requestId?: string; /** Any additional context information */ [key: string]: unknown; } /** * Error handler options */ export interface ErrorHandlerOptions { /** The context of the operation that caused the error */ context?: ErrorContext; /** The name of the operation being performed */ operation: string; /** The input that caused the error */ input?: unknown; /** Whether to rethrow the error after handling */ rethrow?: boolean; /** Custom error code to use when creating an McpError */ errorCode?: BaseErrorCode; /** Custom error mapper function */ errorMapper?: (error: unknown) => Error; /** Whether to include stack traces in logs */ includeStack?: boolean; /** Whether this is a critical error that should abort operations */ critical?: boolean; } /** * Base error mapping rule */ export interface BaseErrorMapping { /** Pattern to match in the error message */ pattern: string | RegExp; /** Error code for mapped errors */ errorCode: BaseErrorCode; /** Custom error message template */ messageTemplate?: string; } /** * Error mapping configuration */ export interface ErrorMapping<T extends Error = Error> extends BaseErrorMapping { /** Factory function to create the mapped error */ factory: (error: unknown, context?: Record<string, unknown>) => T; /** Additional context to merge with error context */ additionalContext?: Record<string, unknown>; } /** * Error handler utility class with various error handling methods */ export declare class ErrorHandler { /** * Determine the appropriate error code for an error based on patterns and type * @param error The error to classify * @returns The appropriate error code */ static determineErrorCode(error: unknown): BaseErrorCode; /** * Handle operation errors with consistent logging and transformation * @param error The error that occurred * @param options Error handling options * @returns The transformed error */ static handleError(error: unknown, options: ErrorHandlerOptions): Error; /** * Map an error to a specific error type based on error message patterns * @param error The error to map * @param mappings Array of pattern and factory mappings * @param defaultFactory Default factory function if no pattern matches * @returns The mapped error */ static mapError<T extends Error>(error: unknown, mappings: ErrorMapping<T>[], defaultFactory?: (error: unknown, context?: Record<string, unknown>) => T): T | Error; /** * Format an error for consistent response structure * @param error The error to format * @returns Formatted error object */ static formatError(error: unknown): Record<string, unknown>; /** * Safely execute a function and handle any errors * @param fn Function to execute * @param options Error handling options * @returns The result of the function or error */ static tryCatch<T>(fn: () => Promise<T> | T, options: ErrorHandlerOptions): Promise<T>; }