-
-
Notifications
You must be signed in to change notification settings - Fork 69
Description
Describe the bug
a undefined not handle by prettyFormatErrorObj
then call the getErrorTrace
.
To Reproduce
this is a large project, so i checked into the code. and finded the bug.
the error throw from this line :
tslog/src/runtime/nodejs/index.ts
Line 129 in 9a5d156
const errorStackStr = getErrorTrace(error as Error).map((stackFrame) => { |
it tell me Cannot read properties of undefined (reading 'map') at prettyFormatErrorObj
.
so , we can kwon that , the return of getErrorTrace()
is undefined .
when we see the function getErrorTrace()
we can find that :
tslog/src/runtime/nodejs/index.ts
Lines 64 to 71 in 9a5d156
export function getErrorTrace(error: Error): IStackFrame[] { | |
return (error as Error)?.stack?.split("\n")?.reduce((result: IStackFrame[], line: string) => { | |
if (line.includes(" at ")) { | |
result.push(stackLineToStackFrame(line)); | |
} | |
return result; | |
}, []) as IStackFrame[]; | |
} |
the function getErrorTrace()
is return undefined | IStackFrame[]
not IStackFrame[]
.
because the function use ?.
chain .
and after i search all code , i find that the browser version code was fixed this issue.
tslog/src/runtime/browser/index.ts
Lines 64 to 72 in 9a5d156
export function getErrorTrace(error: Error): IStackFrame[] { | |
return ((error as Error)?.stack?.split("\n") ?? []) | |
?.filter((line: string) => !line.includes("Error: ")) | |
?.reduce((result: IStackFrame[], line: string) => { | |
result.push(stackLineToStackFrame(line)); | |
return result; | |
}, []) as IStackFrame[]; | |
} |
it fix by a ?? []
simply .
Expected behavior
simple write code like browser , then all will work.
the right code maybe like here :
export function getErrorTrace(error: Error): IStackFrame[] {
return ((error as Error)?.stack?.split("\n") ?? []).reduce((result: IStackFrame[], line: string) => {
if (line.includes(" at ")) {
result.push(stackLineToStackFrame(line));
}
return result;
}, []) as IStackFrame[];
}
Node.js Version
v20.12.2
OS incl. Version
win10