keroosha.cybergulag.today/quartz/trace.ts

32 lines
662 B
TypeScript
Raw Normal View History

import chalk from "chalk"
const rootFile = /.*at file:/
export function trace(msg: string, err: Error) {
const stack = err.stack
console.log()
2023-07-23 03:27:41 +03:00
console.log(
2023-07-23 21:49:26 +03:00
"\n" +
chalk.bgRed.black.bold(" ERROR ") +
"\n" +
2023-07-23 03:27:41 +03:00
chalk.red(` ${msg}`) +
(err.message.length > 0 ? `: ${err.message}` : ""),
)
if (!stack) {
return
}
let reachedEndOfLegibleTrace = false
2023-07-23 03:27:41 +03:00
for (const line of stack.split("\n").slice(1)) {
if (reachedEndOfLegibleTrace) {
break
}
if (!line.includes("node_modules")) {
console.log(` ${line}`)
if (rootFile.test(line)) {
reachedEndOfLegibleTrace = true
}
}
}
}