tslog icon indicating copy to clipboard operation
tslog copied to clipboard

Bug: [BUG] `Cannot read properties of undefined (reading 'map') at prettyFormatErrorObj`

Open Lyoko-Jeremie opened this issue 1 year ago • 1 comments

Describe the bug

a undefined not handle by prettyFormatErrorObj then call the getErrorTrace .

Screenshots 图片

To Reproduce

this is a large project, so i checked into the code. and finded the bug.

the error throw from this line :

https://github.com/fullstack-build/tslog/blob/9a5d15696ae813142b64a21f42987e59e7115448/src/runtime/nodejs/index.ts#L129

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 :

https://github.com/fullstack-build/tslog/blob/9a5d15696ae813142b64a21f42987e59e7115448/src/runtime/nodejs/index.ts#L64-L71

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.

https://github.com/fullstack-build/tslog/blob/9a5d15696ae813142b64a21f42987e59e7115448/src/runtime/browser/index.ts#L64-L72

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

Lyoko-Jeremie avatar Dec 17 '24 08:12 Lyoko-Jeremie

PR created on there : https://github.com/fullstack-build/tslog/pull/312

Lyoko-Jeremie avatar Dec 23 '24 06:12 Lyoko-Jeremie