All files / src/mcp/transport errors.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                      10x 10x                                     11x 11x 11x      
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module MCP/transport/errors
 * @description Typed errors for MCP transport layer.
 */
 
/**
 * Typed error thrown when the MCP gateway returns 401 (session expired).
 * Callers can detect this with `instanceof MCPSessionExpiredError` to trigger re-authentication.
 */
export class MCPSessionExpiredError extends Error {
  /**
   * Construct a new session-expired error.
   *
   * @param statusText - Raw HTTP status text returned by the gateway with the 401.
   */
  constructor(statusText: string) {
    super(`MCP session expired (401): ${statusText}`);
    this.name = 'MCPSessionExpiredError';
  }
}
 
/**
 * Typed error thrown when the MCP gateway returns 429 (rate limited).
 * Carries the parsed `retryAfterMs` delay so callers can honour the back-off period.
 * `retryAfterMs` is 0 when no Retry-After / X-Retry-After header was present.
 */
export class MCPRateLimitError extends Error {
  /** Suggested back-off delay in milliseconds parsed from the gateway response. */
  readonly retryAfterMs: number;
  /**
   * Construct a new rate-limit error.
   *
   * @param retryAfterMs - Parsed Retry-After delay in ms (0 when no header was present).
   * @param message - Human-readable error message including endpoint and delay.
   */
  constructor(retryAfterMs: number, message: string) {
    super(message);
    this.name = 'MCPRateLimitError';
    this.retryAfterMs = retryAfterMs;
  }
}