Open edspencer opened 1 month ago
Slightly more context: it looks like calling reject()
without a reason is supported when the promise is on the client side - see https://react.dev/reference/react/use#dealing-with-rejected-promises
I'm guessing that this may be something to do with how Flight deals with promise resolution.
.catch(e=>{ return "no new message found."; });Maybe you need to throw a catch to catch him
.catch(e=>{ return "no new message found."; });Maybe you need to throw a catch to catch him
Yes, you can write that code, but it's non-obvious that you need to, and if the Promise that rejects is not in your own codebase then it may be difficult/impossible to do that. Also, it's not going to trigger your error boundary, so now you have 2 code paths to handle errors.
I think this is a bug as it doesn't seem reasonable that rejecting a promise without an argument (which is a valid use of reject()
) should crash the process and halt the response. There are workarounds - the catch you mention and also passing an Error instance to reject() (you can even just call reject({digest: 'something'})
as it's the .digest
property the code is looking for), but I lost of few hours of my life figuring this out and I can see it affecting others too.
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/still-cherry-8wks5w
To Reproduce
TypeError: Cannot read properties of undefined (reading 'digest')
error in the terminalIt boils down to a code block like this:
If that
promiseThatWillReject
calls itsreject()
function without any argument, the bug is encountered.Current vs. Expected behavior
Rejecting a promise without a rejection reason causes the Next response to crash with an error inside
create-error-handler.tsx
. This causes the page response to never finish. The error does not bubble up to the ErrorBoundary, and the page just breaks.I expect the rejected promise to trigger my ErrorBoundary, or at least give me some kind of message telling me I need to provide an argument to my
reject()
call.Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure, Developer Experience
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local)
Additional context
This appears to be because
create-error-handler.tsx
expects itserr: any
argument to be present, but when youreject()
a promise without an argument, this does not happen (err
is undefined).To debug, I cloned nextjs, copied my app inside of it as per the debugging instructions and debugged with VS Code. The issue starts inside of
react-server-dom-webpack-server.edge.development.js
, which passes the (in this case undefined)reason
tologRecoverableError
. I guess this part is React code, rather than Next code. That part seems reasonable, as does the next call which callsrequestStorage.run(undefined, onError, error)
(theerror
here is undefined).The error is caused when
create-error-handler.tsx
attempts to readerr.digest
whenerr
is undefined. This issue is occurring at the intersection of React and Next, but it looks like it should be addressed on the Next side, specifically by not assuming thaterr
is defined. Perhaps just defaultingerr
to anew Error()
insidecreate-error-handler.tsx
would be a reasonable solution.The terminal error looks like this: