question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hidden error with await rejected promise inside getStaticPaths

See original GitHub issue

What version of Next.js are you using?

12.0.0

What version of Node.js are you using?

14.16.0, 15.9.0

What browser are you using?

any browser

What operating system are you using?

macOS

How are you deploying your application?

next build

Describe the Bug

if in async function getStaticPaths error raised in some nested async function, called with await, like this

export const getStaticPaths = async () => {
  await Promise.reject('Error hidden');
  // throw new Error('Raised error');
};

then when execute next build in console would be output just

 Build optimization failed: found page without a React Component as default export in pages/dyn/[num]

instead normal error message. If throw error directly - then log is fine

Expected Behavior

output concrete error message

To Reproduce

https://stackblitz.com/edit/nextjs-pnhrbs?file=pages/dyn/[num].jsx

To reproduce run: next build

enough

export const getStaticPaths = async () => {
  await Promise.reject('Error hidden'); // any rejected promise with message
  // throw new Error('Raised error');
};

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
urffincommented, Dec 7, 2021

await promise in that case would be enough I think. If the promise fails, it will just throw its error, which is caught by Next.js

No, this work only if promise rejected with new Error, if reject reason any other value, for example string - output just message about Build optimization failed: found page without a React Component as default export in …

This is a problem to detect real error reason.

1reaction
balazsorban44commented, Dec 6, 2021

It is generally a good idea to reject with an Error if you have control over that.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject#description

Currently, this is why you are getting that message. Next.js checks if the thrown error is an instance of Error: https://github.com/vercel/next.js/blob/cffd209a677db9c953c15e4fc8e168a278d3908c/packages/next/build/index.ts#L1027

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wait until all promises complete even if some rejected
The explicit way to to resolve this issue is to simply call .catch on the internal promises, and return the error from their...
Read more >
Handling those unhandled promise rejections with JS async ...
You put your code inside an async function in order to use await calls; One of your await ed functions fails (i.e. rejects...
Read more >
Handling Promise rejection with catch while using await
In this article, we will try to understand how we may handle Promise's rejection with a try/catch block while using await inside an ......
Read more >
how do you resolve application error a client side exception has ...
Side note: Promise gotchas ... This code misunderstands the above asynchronous issues. Specifically, $.ajax() doesn't freeze the code while it checks the '/ ......
Read more >
Data Fetching: getStaticPaths - Next.js
API reference for `getStaticPaths`. Learn how to fetch data and generate static pages with `getStaticPaths`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found