Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
7e378928
Unverified
Commit
7e378928
authored
Jul 05, 2024
by
Delba de Oliveira
Committed by
GitHub
Jul 05, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs: Update unstable_rethrow example (#67494)
re:
https://x.com/jamannnnnn/status/1809133455592067373
parent
1268f99e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
15 deletions
+11
-15
unstable_rethrow.mdx
...02-app/02-api-reference/04-functions/unstable_rethrow.mdx
+11
-15
No files found.
docs/02-app/02-api-reference/04-functions/unstable_rethrow.mdx
View file @
7e378928
...
@@ -3,33 +3,32 @@ title: unstable_rethrow (experimental)
...
@@ -3,33 +3,32 @@ title: unstable_rethrow (experimental)
description
:
API
Reference
for
the
unstable_rethrow
function
.
description
:
API
Reference
for
the
unstable_rethrow
function
.
---
---
`unstable_rethrow`
can
be
used
to
avoid
catching
internal
errors
thrown
by
Next
.
js
when
attempting
to
handle
errors
thrown
from
your
own
application
.
`unstable_rethrow`
can
be
used
to
avoid
catching
internal
errors
thrown
by
Next
.
js
when
attempting
to
handle
errors
thrown
in
your
application
code
.
For
example
,
given
the
following
code
:
For
example
,
calling
the
`notFound`
function
will
throw
an
internal
Next
.
js
error
and
render
the
[
`not-found.js`
](
/
docs
/
app
/
api
-
reference
/
file
-
conventions
/
not
-
found
)
component
.
However
,
if
used
inside
a
`try/catch`
block
,
the
error
will
be
caught
,
preventing
`not-found.js`
from
rendering
:
```
jsx
```
tsx filename="@/app/ui/component.tsx"
import { notFound } from 'next/navigation'
import { notFound } from 'next/navigation'
export default async function
Component
() {
export default async function
Page
() {
try {
try {
notFound(
)
const post = await fetch('https://.../posts/1').then((res) => res.json()
)
await getData
()
if (!post) notFound
()
} catch (err) {
} catch (err) {
console.error(err)
console.error(err)
}
}
}
}
```
```
In
this
scenario
,
`console.error`
will
pick
up
the
error
thrown
by
`notFound`
,
which
won
't be useful for logging and will prevent
You
can
use
`unstable_rethrow`
API
to
re
-
throw
the
internal
error
and
continue
with
the
expected
behavior
:
the `not-found.js` page from rendering. Instead, you can do this:
```
jsx
```
tsx filename="@/app/ui/component.tsx"
import { notFound, unstable_rethrow } from 'next/navigation'
import { notFound, unstable_rethrow } from 'next/navigation'
export default async function
Component
() {
export default async function
Page
() {
try {
try {
notFound(
)
const post = await fetch('https://.../posts/1').then((res) => res.json()
)
await getData
()
if (!post) notFound
()
} catch (err) {
} catch (err) {
unstable_rethrow(err)
unstable_rethrow(err)
console.error(err)
console.error(err)
...
@@ -37,9 +36,6 @@ export default async function Component() {
...
@@ -37,9 +36,6 @@ export default async function Component() {
}
}
```
```
If the caught error is meant to be handled by Next.js, `unstable_rethrow` will re-throw the error instead of letting it reach your
error handling code.
The
following
Next
.
js
APIs
rely
on
throwing
an
error
which
should
be
rethrown
and
handled
by
Next
.
js
itself
:
The
following
Next
.
js
APIs
rely
on
throwing
an
error
which
should
be
rethrown
and
handled
by
Next
.
js
itself
:
-
[
`notFound()`
](
/
docs
/
app
/
api
-
reference
/
functions
/
not
-
found
)
-
[
`notFound()`
](
/
docs
/
app
/
api
-
reference
/
functions
/
not
-
found
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment