All files / src tool-result.ts

78.26% Statements 18/23
70.83% Branches 17/24
100% Functions 3/3
78.26% Lines 18/23

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 61 62 63 64 65 66 67 68 69 70 71 72    3x     13x         3x 7x 2x 2x               5x 2x     3x       3x 2x     1x       1x     3x 9x 7x   7x                                 2x                    
import { SrcmapError } from "./srcmap-client.js";
 
export const toTextResult = (
  text: string,
  structuredContent?: Record<string, unknown>,
) => ({
  content: [{ type: "text" as const, text }],
  ...(structuredContent ? { structuredContent } : {}),
});
 
const getRecoverySuggestion = (code: string, message: string): string | null => {
  if (code === "FETCH_ERROR") {
    Eif (message.includes("HTTP 404")) {
      return "URL not found. Verify the bundle URL is correct and publicly accessible.";
    }
    if (message.includes("HTTP 403") || message.includes("HTTP 401")) {
      return "Access denied. The URL may require authentication or be behind a CDN restriction.";
    }
    return "Network error. Verify the URL is correct and the server is reachable.";
  }
 
  if (code === "NOT_FOUND") {
    return "No mapping found at this position. Try nearby positions or verify line/column are 0-based.";
  }
 
  Iif (code === "PARSE_ERROR") {
    return "Invalid source map. Verify the file is a valid source map (JSON with version, mappings, sources fields).";
  }
 
  if (code === "IO_ERROR") {
    return "File not found or not readable. Check the file path.";
  }
 
  Iif (code === "PATH_TRAVERSAL") {
    return "Source name contains path traversal. The source map may be malformed.";
  }
 
  return null;
};
 
export const toErrorResult = (error: unknown) => {
  if (error instanceof SrcmapError) {
    const suggestion = getRecoverySuggestion(error.code, error.message);
 
    return {
      content: [
        {
          type: "text" as const,
          text: [
            `srcmap error: ${error.message}`,
            `Code: ${error.code}`,
            suggestion ? `\nRecovery: ${suggestion}` : "",
          ]
            .filter(Boolean)
            .join("\n"),
        },
      ],
      isError: true,
    };
  }
 
  return {
    content: [
      {
        type: "text" as const,
        text: error instanceof Error ? error.message : String(error),
      },
    ],
    isError: true,
  };
};