Unverified Commit e6b12321 authored by Josh Story's avatar Josh Story Committed by GitHub

remove unimplemented onError errorInfo argument (#70531)

RSC has never supported the `errorInfo` argument to `onError` unlike
SSR. This PR updates our usage of onError in RSC contexts to clarify
that this value does not exist

This also updates the next interned types for react-dom/server to
reflect that `renderTo...` and `prerender` support the `ErrorInfo`
second arg and `resume` does not.
parent 3a832f1e
...@@ -22,7 +22,7 @@ import type { DeepReadonly } from '../../shared/lib/deep-readonly' ...@@ -22,7 +22,7 @@ import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import type { BaseNextRequest, BaseNextResponse } from '../base-http' import type { BaseNextRequest, BaseNextResponse } from '../base-http'
import type { IncomingHttpHeaders } from 'http' import type { IncomingHttpHeaders } from 'http'
import React, { type JSX } from 'react' import React, { type ErrorInfo, type JSX } from 'react'
import RenderResult, { import RenderResult, {
type AppPageRenderResultMetadata, type AppPageRenderResultMetadata,
...@@ -1959,13 +1959,13 @@ async function prerenderToStream( ...@@ -1959,13 +1959,13 @@ async function prerenderToStream(
dynamicTracking, dynamicTracking,
} }
let SSRIsDynamic = false let SSRIsDynamic = false
function SSROnError(err: unknown) { function SSROnError(err: unknown, errorInfo?: ErrorInfo) {
if (err === abortReason || isPrerenderInterruptedError(err)) { if (err === abortReason || isPrerenderInterruptedError(err)) {
SSRIsDynamic = true SSRIsDynamic = true
return return
} }
return htmlRendererErrorHandler(err) return htmlRendererErrorHandler(err, errorInfo)
} }
function SSROnPostpone(reason: string) { function SSROnPostpone(reason: string) {
...@@ -2256,7 +2256,7 @@ async function prerenderToStream( ...@@ -2256,7 +2256,7 @@ async function prerenderToStream(
const err = new DynamicServerError( const err = new DynamicServerError(
`Route ${staticGenerationStore.route} couldn't be rendered statically because it used IO that was not cached in a Server Component. See more info here: https://nextjs.org/docs/messages/dynamic-io` `Route ${staticGenerationStore.route} couldn't be rendered statically because it used IO that was not cached in a Server Component. See more info here: https://nextjs.org/docs/messages/dynamic-io`
) )
serverComponentsErrorHandler(err, {}) serverComponentsErrorHandler(err)
throw err throw err
} }
......
import type { ErrorInfo } from 'react'
import stringHash from 'next/dist/compiled/string-hash' import stringHash from 'next/dist/compiled/string-hash'
import { formatServerError } from '../../lib/format-server-error' import { formatServerError } from '../../lib/format-server-error'
import { SpanStatusCode, getTracer } from '../lib/trace/tracer' import { SpanStatusCode, getTracer } from '../lib/trace/tracer'
...@@ -10,27 +12,24 @@ declare global { ...@@ -10,27 +12,24 @@ declare global {
var __next_log_error__: undefined | ((err: unknown) => void) var __next_log_error__: undefined | ((err: unknown) => void)
} }
type ErrorHandler = (err: unknown, errorInfo?: unknown) => string | undefined type RSCErrorHandler = (err: unknown) => string | undefined
type SSRErrorHandler = (
err: unknown,
errorInfo?: ErrorInfo
) => string | undefined
export type DigestedError = Error & { digest: string } export type DigestedError = Error & { digest: string }
export function createFlightReactServerErrorHandler( export function createFlightReactServerErrorHandler(
dev: boolean, dev: boolean,
onReactServerRenderError: (err: any) => void onReactServerRenderError: (err: any) => void
): ErrorHandler { ): RSCErrorHandler {
return (err: any, errorInfo?: unknown) => { return (err: any) => {
// If the error already has a digest, respect the original digest, // If the error already has a digest, respect the original digest,
// so it won't get re-generated into another new error. // so it won't get re-generated into another new error.
if (!err.digest) { if (!err.digest) {
// TODO-APP: look at using webcrypto instead. Requires a promise to be awaited. // TODO-APP: look at using webcrypto instead. Requires a promise to be awaited.
err.digest = stringHash( err.digest = stringHash(err.message + err.stack || '').toString()
err.message +
(typeof errorInfo === 'object' &&
errorInfo !== null &&
'stack' in errorInfo
? errorInfo.stack
: err.stack || '')
).toString()
} }
// If the response was closed, we don't need to log the error. // If the response was closed, we don't need to log the error.
...@@ -75,15 +74,13 @@ export function createHTMLReactServerErrorHandler( ...@@ -75,15 +74,13 @@ export function createHTMLReactServerErrorHandler(
reactServerErrors: Map<string, DigestedError>, reactServerErrors: Map<string, DigestedError>,
silenceLogger: boolean, silenceLogger: boolean,
onReactServerRenderError: undefined | ((err: any) => void) onReactServerRenderError: undefined | ((err: any) => void)
): ErrorHandler { ): RSCErrorHandler {
return (err: any, errorInfo: any) => { return (err: any) => {
// If the error already has a digest, respect the original digest, // If the error already has a digest, respect the original digest,
// so it won't get re-generated into another new error. // so it won't get re-generated into another new error.
if (!err.digest) { if (!err.digest) {
// TODO-APP: look at using webcrypto instead. Requires a promise to be awaited. // TODO-APP: look at using webcrypto instead. Requires a promise to be awaited.
err.digest = stringHash( err.digest = stringHash(err.message + (err.stack || '')).toString()
err.message + (errorInfo?.stack || err.stack || '')
).toString()
} }
// If the response was closed, we don't need to log the error. // If the response was closed, we don't need to log the error.
...@@ -146,9 +143,9 @@ export function createHTMLErrorHandler( ...@@ -146,9 +143,9 @@ export function createHTMLErrorHandler(
reactServerErrors: Map<string, DigestedError>, reactServerErrors: Map<string, DigestedError>,
allCapturedErrors: Array<unknown>, allCapturedErrors: Array<unknown>,
silenceLogger: boolean, silenceLogger: boolean,
onHTMLRenderSSRError: (err: any) => void onHTMLRenderSSRError: (err: any, errorInfo?: ErrorInfo) => void
): ErrorHandler { ): SSRErrorHandler {
return (err: any, errorInfo: any) => { return (err: any, errorInfo?: ErrorInfo) => {
let isSSRError = true let isSSRError = true
// If the error already has a digest, respect the original digest, // If the error already has a digest, respect the original digest,
...@@ -165,7 +162,7 @@ export function createHTMLErrorHandler( ...@@ -165,7 +162,7 @@ export function createHTMLErrorHandler(
} }
} else { } else {
err.digest = stringHash( err.digest = stringHash(
err.message + (errorInfo?.stack || err.stack || '') err.message + (errorInfo?.componentStack || err.stack || '')
).toString() ).toString()
} }
...@@ -215,7 +212,7 @@ export function createHTMLErrorHandler( ...@@ -215,7 +212,7 @@ export function createHTMLErrorHandler(
// HTML errors contain RSC errors as well, filter them out before reporting // HTML errors contain RSC errors as well, filter them out before reporting
isSSRError isSSRError
) { ) {
onHTMLRenderSSRError(err) onHTMLRenderSSRError(err, errorInfo)
} }
} }
......
declare module 'react-dom/server.edge' { declare module 'react-dom/server.edge' {
import type { JSX } from 'react' import type { ErrorInfo, JSX } from 'react'
/** /**
* https://github.com/facebook/react/blob/aec521a96d3f1bebc2ba38553d14f4989c6e88e0/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js#L329-L333 * https://github.com/facebook/react/blob/aec521a96d3f1bebc2ba38553d14f4989c6e88e0/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js#L329-L333
*/ */
...@@ -42,7 +42,7 @@ declare module 'react-dom/server.edge' { ...@@ -42,7 +42,7 @@ declare module 'react-dom/server.edge' {
bootstrapModules?: Array<string | BootstrapScriptDescriptor> bootstrapModules?: Array<string | BootstrapScriptDescriptor>
progressiveChunkSize?: number progressiveChunkSize?: number
signal?: AbortSignal signal?: AbortSignal
onError?: (error: unknown) => string | undefined onError?: (error: unknown, errorInfo: ErrorInfo) => string | undefined
onPostpone?: (reason: string) => void onPostpone?: (reason: string) => void
unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor
importMap?: { importMap?: {
...@@ -71,7 +71,7 @@ declare module 'react-dom/server.edge' { ...@@ -71,7 +71,7 @@ declare module 'react-dom/server.edge' {
} }
declare module 'react-dom/static.edge' { declare module 'react-dom/static.edge' {
import type { JSX } from 'react' import type { ErrorInfo, JSX } from 'react'
/** /**
* https://github.com/facebook/react/blob/aec521a96d3f1bebc2ba38553d14f4989c6e88e0/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js#L329-L333 * https://github.com/facebook/react/blob/aec521a96d3f1bebc2ba38553d14f4989c6e88e0/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js#L329-L333
*/ */
...@@ -94,7 +94,7 @@ declare module 'react-dom/static.edge' { ...@@ -94,7 +94,7 @@ declare module 'react-dom/static.edge' {
bootstrapModules?: Array<string | BootstrapScriptDescriptor> bootstrapModules?: Array<string | BootstrapScriptDescriptor>
progressiveChunkSize?: number progressiveChunkSize?: number
signal?: AbortSignal signal?: AbortSignal
onError?: (error: unknown) => string | undefined onError?: (error: unknown, errorInfo: ErrorInfo) => string | undefined
onPostpone?: (reason: string) => void onPostpone?: (reason: string) => void
unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor
importMap?: { importMap?: {
......
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