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 73 74 75 76 77 78 79 80 81 | 3x 8x 7x 12x 3x 17x 4x 1x 3x 2x 1x 13x 1x 12x 1x 11x 1x 10x 1x 9x 3x 19x 17x 17x 2x 3x 78x 18x | import { SrcmapError } from "./srcmap-client.js";
export const formatSize = (bytes: number): string => {
if (bytes < 1024) return `${bytes} B`;
Eif (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};
export const toTextResult = (text: string, structuredContent?: object) => ({
content: [{ type: "text" as const, text }],
...(structuredContent ? { structuredContent: structuredContent as Record<string, unknown> } : {}),
});
const getRecoverySuggestion = (code: string, message: string): string | null => {
if (code === "FETCH_ERROR") {
if (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.";
}
if (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.";
}
if (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,
};
};
/** Wraps a tool handler so any thrown error becomes an error result instead of a rejected call. */
export const guarded =
<A, T>(handler: (args: A) => Promise<T>) =>
(args: A): Promise<T | ReturnType<typeof toErrorResult>> =>
handler(args).catch(toErrorResult);
|