export function formatSourceLocation(loc) {
if (loc == null || typeof loc === "symbol") {
return "generated";
}
return `${loc.start.line}:${loc.start.column}-${loc.end.line}:${loc.end.column}`;
}
export function debugPrintError(error) {
const lines = [];
if (error.details && error.details.length > 0) {
for (const detail of error.details) {
lines.push("Error:");
lines.push(` category: ${detail.category ?? "unknown"}`);
lines.push(` severity: ${detail.severity ?? "unknown"}`);
lines.push(` reason: ${JSON.stringify(detail.reason ?? "")}`);
if (detail.description != null) {
lines.push(` description: ${JSON.stringify(detail.description)}`);
} else {
lines.push(` description: null`);
}
const loc =
typeof detail.primaryLocation === "function"
? detail.primaryLocation()
: detail.loc;
lines.push(` loc: ${formatSourceLocation(loc)}`);
const suggestions = detail.suggestions ?? [];
if (suggestions.length === 0) {
lines.push(` suggestions: []`);
} else {
lines.push(` suggestions:`);
for (const s of suggestions) {
lines.push(` - op: ${s.op}`);
lines.push(` range: [${s.range[0]}, ${s.range[1]}]`);
lines.push(` description: ${JSON.stringify(s.description)}`);
if (s.text != null) {
lines.push(` text: ${JSON.stringify(s.text)}`);
}
}
}
if (
detail.options &&
detail.options.details &&
detail.options.details.length > 0
) {
lines.push(` details:`);
for (const d of detail.options.details) {
if (d.kind === "error") {
lines.push(` - kind: error`);
lines.push(` loc: ${formatSourceLocation(d.loc)}`);
lines.push(` message: ${JSON.stringify(d.message)}`);
} else if (d.kind === "hint") {
lines.push(` - kind: hint`);
lines.push(` message: ${JSON.stringify(d.message)}`);
}
}
}
}
} else {
lines.push("Error:");
lines.push(` message: ${JSON.stringify(error.message)}`);
}
return lines.join("\n") + "\n";
}