Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Lyoko-Jeremie opened this issue Dec 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Lyoko-Jeremie
Copy link

Lyoko-Jeremie commented Dec 17, 2024

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

@Lyoko-Jeremie Lyoko-Jeremie added the bug Something isn't working label Dec 17, 2024
Lyoko-Jeremie added a commit to Lyoko-Jeremie/tslog that referenced this issue Dec 23, 2024
fix Cannot read properties of undefined (reading 'map') at prettyFormatErrorObj .
fullstack-build#311
@Lyoko-Jeremie
Copy link
Author

PR created on there : #312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant