Skip to content

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

Open
@Lyoko-Jeremie

Description

@Lyoko-Jeremie

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 :

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 :

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions