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

no stack trace for App Promise Reject with non-Error reasons #77351

Open
samijaber opened this issue Mar 20, 2025 · 2 comments
Open

no stack trace for App Promise Reject with non-Error reasons #77351

samijaber opened this issue Mar 20, 2025 · 2 comments
Labels
Error Handling Related to handling errors (e.g., error.tsx, global-error.tsx).

Comments

@samijaber
Copy link
Contributor

Link to the code that reproduces this issue

https://github.com/samijaber/nextjs-reject-bug-repro/

To Reproduce

  1. https://github.com/samijaber/nextjs-reject-bug-repro/
  2. npm run dev
  3. open / and see error message
  4. toggle the two different promises to compare stack traces https://github.com/samijaber/nextjs-reject-bug-repro/blob/ea125b442fbb275efcb3341fa70a4f2199395708/src/app/page.tsx#L112-L117

Current vs. Expected behavior

When a Promise reject has an Error reason:

Image

When a Promise reject has a non-Error reason (an arbitrary object):

Image

note how we lose information on where the rejection is coming from.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Dec  6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.11.0
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 15.2.3 // Latest available version is detected (15.2.3).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.8.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Error Handling

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@github-actions github-actions bot added the Error Handling Related to handling errors (e.g., error.tsx, global-error.tsx). label Mar 20, 2025
@samijaber
Copy link
Contributor Author

Bug repro commit samijaber/nextjs-reject-bug-repro@ea125b4

@milkbottle0305
Copy link

milkbottle0305 commented Mar 21, 2025

In Next.js, when you reject a Promise with a non-Error object, the stack trace is not provided, making debugging harder. This is because JavaScript only includes a stack trace for Error objects. The solution is to reject the Promise with an Error object.

Example:

// Incorrect approach
return Promise.reject({ message: 'Something went wrong' });

// Correct approach
return Promise.reject(new Error('Something went wrong'));

This way, the stack trace will be included, making error tracking possible.
Reference: https://nextjs.org/docs/messages/threw-undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Handling Related to handling errors (e.g., error.tsx, global-error.tsx).
Projects
None yet
Development

No branches or pull requests

2 participants