Unverified Commit ae63c57a authored by Joseph's avatar Joseph Committed by GitHub

docs: type error.js error prop as unknown (#100232)

## Summary

Follow-up to #100196. `error.js` and `global-error.js` receive whatever
was thrown: Server Component errors arrive as an `Error`, but a value
thrown in a Client Component is passed through as-is. The docs annotated
the prop as `Error & { digest?: string }` and described it as always an
`Error`.

- `error.mdx` and Getting Started > Error Handling: type `error` as
`unknown` in the `error.tsx` and `global-error.tsx` examples, and
describe the prop as the thrown value, with a note to narrow before
reading `message` or `digest`.
- `with-stripe-typescript`: the three `error.tsx` boundaries narrow with
`instanceof Error`. The component is renamed from `Error` to `ErrorPage`
so it no longer shadows the global.

<!-- NEXT_JS_LLM -->
parent 8e870bc4
......@@ -217,7 +217,7 @@ export default function ErrorPage({
error,
retry,
}: {
error: Error & { digest?: string }
error: unknown
retry: () => void
}) {
useEffect(() => {
......@@ -407,7 +407,7 @@ export default function GlobalError({
error,
retry,
}: {
error: Error & { digest?: string }
error: unknown
retry: () => void
}) {
return (
......
......@@ -26,7 +26,7 @@ export default function Error({
error,
retry,
}: {
error: Error & { digest?: string }
error: unknown
retry: () => void
}) {
useEffect(() => {
......@@ -101,7 +101,7 @@ In the [component hierarchy](/docs/app/getting-started/project-structure#compone
#### `error`
An instance of an [`Error`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error) object forwarded to the `error.js` Client Component.
The value that was thrown, forwarded to the `error.js` Client Component. Errors from Server Components arrive as an [`Error`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error) instance, while a value thrown in a Client Component is passed as-is and may not be an `Error`. Narrow it, for example with `instanceof Error`, before reading properties like `message` or `digest`.
> **Good to know:** During development, the `Error` object forwarded to the client will be serialized and include the `message` of the original error for easier debugging. However, **this behavior is different in production** to avoid leaking potentially sensitive details included in the error to the client.
......@@ -127,7 +127,7 @@ export default function Error({
error,
retry,
}: {
error: Error & { digest?: string }
error: unknown
retry: () => void
}) {
return (
......@@ -173,7 +173,7 @@ export default function GlobalError({
error,
retry,
}: {
error: Error & { digest?: string }
error: unknown
retry: () => void
}) {
return (
......
"use client";
export default function Error({ error }: { error: Error }) {
return <h2>{error.message}</h2>;
export default function ErrorPage({ error }: { error: unknown }) {
const message = error instanceof Error ? error.message : String(error);
return <h2>{message}</h2>;
}
"use client";
export default function Error({ error }: { error: Error }) {
return <h2>{error.message}</h2>;
export default function ErrorPage({ error }: { error: unknown }) {
const message = error instanceof Error ? error.message : String(error);
return <h2>{message}</h2>;
}
"use client";
export default function Error({ error }: { error: Error }) {
return <h2>{error.message}</h2>;
export default function ErrorPage({ error }: { error: unknown }) {
const message = error instanceof Error ? error.message : String(error);
return <h2>{message}</h2>;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment