forked from weroperking/Betterbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-formatter.ts
More file actions
42 lines (37 loc) · 1.3 KB
/
Copy patherror-formatter.ts
File metadata and controls
42 lines (37 loc) · 1.3 KB
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
import chalk from "chalk";
import { ZodError } from "zod";
export function formatDevError(err: unknown, context: string): string {
if (err instanceof ZodError) {
const lines = [chalk.red(` ✗ Validation error in ${context}`)];
for (const issue of err.issues) {
const path = issue.path.length ? issue.path.join(".") : "root";
lines.push(` ${chalk.dim(path)}: ${chalk.yellow(issue.message)}`);
}
return lines.join("\n");
}
if (err instanceof Error) {
// Highlight the first relevant stack frame
const relevant = err.stack
?.split("\n")
.find((l) => l.includes("betterbase/") || l.includes("src/modules"));
return [
chalk.red(` ✗ ${context}: ${err.message}`),
relevant ? chalk.dim(` ${relevant.trim()}`) : "",
]
.filter(Boolean)
.join("\n");
}
return chalk.red(` ✗ ${context}: ${String(err)}`);
}
/** Pretty-print a schema diff for the dev console */
export function formatDiffForDev(
changes: { type: string; table: string; column?: string; destructive: boolean }[],
): string {
return changes
.map((c) => {
const icon = c.destructive ? chalk.red("⚠") : chalk.green("+");
const detail = c.column ? `${c.table}.${c.column}` : c.table;
return ` ${icon} ${chalk.dim(c.type.replace("_", " ").toLowerCase())} ${chalk.white(detail)}`;
})
.join("\n");
}