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