/** * Common Error Codes for Adapters * * This module provides error code constants for common error scenarios * that adapters encounter. These codes align with the core ERROR_CODES * from @wavespec/types but are exposed at the adapter-kit level * for convenient use when building error patterns. * * Using these constants ensures: * - Consistent error codes across all adapters * - Consistency with the harness error catalog * - Type-safe error code references * - Easier maintenance (centralized definitions) * * ## Code Categories * - Connection Errors: ECONNREFUSED, ENOTFOUND, ECONNRESET, DNS failures * - HTTP Status Codes: 401, 403, 404, 429, 500, 502, 503 * - Protocol Errors: Parse errors, invalid responses, protocol violations * - Timeout Errors: Operation timeouts, request timeouts * - Authentication Errors: Auth failures, authorization failures * - Resource Errors: Tool not found, prompt not found, resource not found * * @module @wavespec/kit/error * @example * ```typescript * import { ERROR_CODES } from "@wavespec/kit/error"; * * const pattern: ErrorPattern = { * match: /econnrefused/i, * code: ERROR_CODES.CONNECTION_REFUSED, * hint: "Server refused connection", * fix: "Check server is running" * }; * ``` */ /** * Common error codes for adapters * * These map to standard harness error codes, making them reusable * across HTTP, MCP, OpenAI Agents, and other adapters. * * All codes are exported as constants for type safety. */ export const ERROR_CODES = { // =================================================================== // CONNECTION ERRORS // =================================================================== /** * Connection refused - server actively rejected connection * Maps to: ADAPTER_CONNECTION_FAILED */ CONNECTION_REFUSED: "ADAPTER_CONNECTION_FAILED", /** * Connection timeout - unable to reach server * Maps to: OPERATION_TIMEOUT */ CONNECTION_TIMEOUT: "OPERATION_TIMEOUT", /** * Connection reset - connection was forcibly closed by server * Maps to: ADAPTER_CONNECTION_FAILED */ CONNECTION_RESET: "ADAPTER_CONNECTION_FAILED", /** * DNS lookup failed - hostname could not be resolved * Maps to: ADAPTER_CONNECTION_FAILED */ DNS_RESOLUTION_FAILED: "ADAPTER_CONNECTION_FAILED", /** * Network unreachable - system cannot reach the network * Maps to: ADAPTER_CONNECTION_FAILED */ NETWORK_UNREACHABLE: "ADAPTER_CONNECTION_FAILED", /** * Host unreachable - specific host cannot be reached * Maps to: ADAPTER_CONNECTION_FAILED */ HOST_UNREACHABLE: "ADAPTER_CONNECTION_FAILED", // =================================================================== // HTTP STATUS CODES (4xx) // =================================================================== /** * HTTP 400 Bad Request - invalid request parameters * Maps to: INVALID_TOOL_ARGUMENTS */ HTTP_400_BAD_REQUEST: "INVALID_TOOL_ARGUMENTS", /** * HTTP 401 Unauthorized - authentication required or failed * Maps to: ADAPTER_CONNECTION_FAILED (auth is connection concern) */ HTTP_401_UNAUTHORIZED: "ADAPTER_CONNECTION_FAILED", /** * HTTP 403 Forbidden - authenticated but not authorized * Maps to: ADAPTER_CONNECTION_FAILED (permission is connection concern) */ HTTP_403_FORBIDDEN: "ADAPTER_CONNECTION_FAILED", /** * HTTP 404 Not Found - resource does not exist * Maps to: TOOL_NOT_FOUND (can be tool, resource, prompt, endpoint) */ HTTP_404_NOT_FOUND: "TOOL_NOT_FOUND", /** * HTTP 408 Request Timeout - server timed out waiting for request * Maps to: OPERATION_TIMEOUT */ HTTP_408_REQUEST_TIMEOUT: "OPERATION_TIMEOUT", /** * HTTP 429 Too Many Requests - rate limited * Maps to: TEST_EXECUTION_ERROR (rate limiting is a test concern) */ HTTP_429_RATE_LIMIT: "TEST_EXECUTION_ERROR", // =================================================================== // HTTP STATUS CODES (5xx) // =================================================================== /** * HTTP 500 Internal Server Error - server bug * Maps to: SERVER_ERROR */ HTTP_500_SERVER_ERROR: "SERVER_ERROR", /** * HTTP 502 Bad Gateway - upstream server error * Maps to: ADAPTER_CONNECTION_FAILED (service unavailable) */ HTTP_502_BAD_GATEWAY: "ADAPTER_CONNECTION_FAILED", /** * HTTP 503 Service Unavailable - server temporarily down * Maps to: ADAPTER_CONNECTION_FAILED (service unavailable) */ HTTP_503_SERVICE_UNAVAILABLE: "ADAPTER_CONNECTION_FAILED", /** * HTTP 504 Gateway Timeout - upstream server timeout * Maps to: OPERATION_TIMEOUT */ HTTP_504_GATEWAY_TIMEOUT: "OPERATION_TIMEOUT", // =================================================================== // PROTOCOL ERRORS // =================================================================== /** * Generic protocol error - invalid protocol response or state * Maps to: MCP_PROTOCOL_ERROR (most specific available) */ PROTOCOL_ERROR: "MCP_PROTOCOL_ERROR", /** * Parse error - could not parse response (JSON, XML, etc.) * Maps to: INVALID_JSON_RESPONSE */ PARSE_ERROR: "INVALID_JSON_RESPONSE", /** * Invalid response - response missing required fields * Maps to: MCP_PROTOCOL_ERROR (protocol violation) */ INVALID_RESPONSE: "MCP_PROTOCOL_ERROR", /** * Malformed response - response structure is invalid * Maps to: MCP_PROTOCOL_ERROR */ MALFORMED_RESPONSE: "MCP_PROTOCOL_ERROR", // =================================================================== // TIMEOUT ERRORS // =================================================================== /** * Operation timeout - operation exceeded configured timeout * Maps to: OPERATION_TIMEOUT */ OPERATION_TIMEOUT: "OPERATION_TIMEOUT", /** * Request timeout - HTTP request exceeded timeout * Maps to: OPERATION_TIMEOUT */ REQUEST_TIMEOUT: "OPERATION_TIMEOUT", /** * Connection timeout - could not establish connection in time * Maps to: OPERATION_TIMEOUT */ CONNECT_TIMEOUT: "OPERATION_TIMEOUT", // =================================================================== // AUTHENTICATION ERRORS // =================================================================== /** * Authentication failed - invalid credentials or token * Maps to: ADAPTER_CONNECTION_FAILED (auth is connection concern) */ AUTHENTICATION_FAILED: "ADAPTER_CONNECTION_FAILED", /** * Authorization failed - insufficient permissions * Maps to: ADAPTER_CONNECTION_FAILED */ AUTHORIZATION_FAILED: "ADAPTER_CONNECTION_FAILED", /** * Invalid token - token is malformed or expired * Maps to: ADAPTER_CONNECTION_FAILED */ INVALID_TOKEN: "ADAPTER_CONNECTION_FAILED", /** * Token expired - authentication token has expired * Maps to: ADAPTER_CONNECTION_FAILED */ TOKEN_EXPIRED: "ADAPTER_CONNECTION_FAILED", // =================================================================== // RESOURCE/ENTITY ERRORS // =================================================================== /** * Tool not found - requested tool does not exist * Maps to: TOOL_NOT_FOUND */ TOOL_NOT_FOUND: "TOOL_NOT_FOUND", /** * Prompt not found - requested prompt does not exist * Maps to: PROMPT_NOT_FOUND */ PROMPT_NOT_FOUND: "PROMPT_NOT_FOUND", /** * Resource not found - requested resource does not exist * Maps to: RESOURCE_NOT_FOUND */ RESOURCE_NOT_FOUND: "RESOURCE_NOT_FOUND", /** * Invalid tool arguments - tool called with wrong arguments * Maps to: INVALID_TOOL_ARGUMENTS */ INVALID_TOOL_ARGUMENTS: "INVALID_TOOL_ARGUMENTS", // =================================================================== // SSL/TLS ERRORS // =================================================================== /** * Certificate validation failed - SSL/TLS certificate invalid * Maps to: ADAPTER_CONNECTION_FAILED (connection concern) */ CERTIFICATE_VALIDATION_FAILED: "ADAPTER_CONNECTION_FAILED", /** * SSL/TLS error - general SSL/TLS error * Maps to: ADAPTER_CONNECTION_FAILED */ SSL_TLS_ERROR: "ADAPTER_CONNECTION_FAILED", /** * Certificate expired - SSL/TLS certificate has expired * Maps to: ADAPTER_CONNECTION_FAILED */ CERTIFICATE_EXPIRED: "ADAPTER_CONNECTION_FAILED", /** * Certificate hostname mismatch - certificate doesn't match hostname * Maps to: ADAPTER_CONNECTION_FAILED */ CERTIFICATE_HOSTNAME_MISMATCH: "ADAPTER_CONNECTION_FAILED", // =================================================================== // GENERIC/FALLBACK ERRORS // =================================================================== /** * Unknown error - error type could not be determined * Maps to: TEST_EXECUTION_ERROR (generic test failure) */ UNKNOWN_ERROR: "TEST_EXECUTION_ERROR", /** * Server error - generic server-side error * Maps to: SERVER_ERROR */ SERVER_ERROR: "SERVER_ERROR", /** * Test execution error - test failed to execute * Maps to: TEST_EXECUTION_ERROR */ TEST_EXECUTION_ERROR: "TEST_EXECUTION_ERROR", } as const; /** * Type for error code strings (for type safety) * * Ensures only valid error codes are used in patterns. */ export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES]; /** * Get the harness error code name from an ERROR_CODES value * * Useful for documentation and debugging. * * @example * ```typescript * const code = ERROR_CODES.CONNECTION_REFUSED; // "ADAPTER_CONNECTION_FAILED" * const name = getErrorCodeName(code); // "ADAPTER_CONNECTION_FAILED" * ``` */ export function getErrorCodeName(code: string): string { for (const [key, value] of Object.entries(ERROR_CODES)) { if (value === code) { return key; } } return code; }