All files / lib errors.ts

100% Statements 60/60
100% Branches 12/12
100% Functions 5/5
100% Lines 60/60

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 611x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 97x 97x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 4x 4x 2x 2x 4x 4x 7x 1x 1x 7x 7x  
"use strict";
/**
 * Error types shared across oag-mcp components.
 *
 * @module errors
 */
 
/**
 * An error carrying a stable machine-readable code and an actionable hint
 * for the person running the CLI.
 */
export class OagMcpError extends Error {
  readonly code: string;
  readonly hint?: string;
  readonly details?: Record<string, unknown>;
 
  /**
   * @param {String} code - stable error code, e.g. "SDK_LAYOUT_UNSUPPORTED"
   * @param {String} message - human-readable description
   * @param {Object} [opts] - optional settings
   * @param {String} [opts.hint] - actionable next step
   * @param {Object} [opts.details] - structured, non-sensitive context
   * @param {Error} [opts.cause] - underlying error
   */
  constructor(
    code: string,
    message: string,
    opts: {
      hint?: string;
      details?: Record<string, unknown>;
      cause?: unknown;
    } = {},
  ) {
    super(message, opts.cause ? { cause: opts.cause } : undefined);
    this.name = "OagMcpError";
    this.code = code;
    this.hint = opts.hint;
    this.details = opts.details;
  }
}
 
/**
 * Format an error for terminal output (stderr), including its hint.
 *
 * @param {Error} err - error to format
 * @return {String} formatted message
 */
export function formatCliError(err: unknown): string {
  if (err instanceof OagMcpError) {
    const lines = [`Error [${err.code}]: ${err.message}`];
    if (err.hint) {
      lines.push(`Hint: ${err.hint}`);
    }
    return lines.join("\n");
  }
  if (err instanceof Error) {
    return `Error: ${err.message}`;
  }
  return `Error: ${String(err)}`;
}