RFC: unstable_rethrow
API
#64076
Replies: 3 comments 1 reply
-
I agree in the importance of this API in addition to checking against both |
Beta Was this translation helpful? Give feedback.
-
I find this to be a reasonable solution if redesigning the public APIs to not throw errors is infeasible. I think it is intuitive that dynamic functions ( async function action() {
try {
...
// This just feels like a dangling promise and a missing `return` before `redirect()`
redirect(...)
} catch(err) {
...
}
} The proposed |
Beta Was this translation helpful? Give feedback.
-
in the server side gives me error while call unstable_rethrow ```
|
Beta Was this translation helpful? Give feedback.
-
Co-authored by: @wyattjoh
Goals
try/catch
blocks around Next.js APIs that rely on throwingBackground
The following Next.js APIs rely on throwing an error which should be rethrown and handled by Next.js itself.:
notFound()
redirect()
permanentRedirect()
Next.js throws errors to communicate navigation intent to interrupt the code execution and React component rendering. These errors propagate up to the browser when streaming to allow the browser to perform the action, change the status code and/or return a different page when running on the server.
If a route segment is marked to throw an error unless it's static, a dynamic function call will also throw an error that should similarly not be caught by the developer. Note that Partial Prerendering (PPR) affects this behavior as well. These APIs are:
cookies()
headers()
searchParams
propertiesExisting issues
Developers are currently asked to call these APIs outside
try/catch
blocks, but this is easy to miss or not always ergonomic:Proposal
Next.js will provide a new API, that will work both on the client and server:
function unstable_rethrow(error: unknown): void | never
, which handles all Next.js errors by re-throwing them to allow Next.js to take over the error handling, and continue execution for all other cases so the developer can provide their own error-handling mechanism.This method should be called at the top of the
catch
block, passing the error object as its only argument. It can also be used within a.catch
handler of a promise.Additionally, if you defined your own
[ErrorBoundary
component](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary),unstable_rethrow
should also be called at the top of thegetDerivedStateFromError
andcomponentDidCatch
methods. Note, if you used the Next.jserror.js
file, Next.js will re-throw those errors for you.It's important to note though that these navigation APIs (like
notFound()
orredirect()
) will always throw when called, this tool is useful but you may not need it. If you ensure that your calls to API's that throw are not wrapped in atry/catch
then you shouldn't requiredunstable_rethrow
.It's important to know that
unstable_rethrow
, if passed an internal Next.js errror, it will throw. This means that any resource cleanup (like clearing intervals, timers, etc) would have to either happen prior to the call tounstable_rethrow
or within afinally
block.Example usage:
Example using a
try/catch
block:Or using a promise's
.catch
handler:Caveats
The
unstable_rethrow
API will not inspect error objects that have their source errors nested. If a library callsredirect()
within atry/catch
block, but then nests the error like:Alternatives
Beta Was this translation helpful? Give feedback.
All reactions