Unverified Commit d6f9e080 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[dev-overlay] Stop appending wrong Owner Stacks to SSR-only shell errors (#77302)

parent 166251da
...@@ -18,17 +18,13 @@ export function setOwnerStack(error: Error, stack: string | null) { ...@@ -18,17 +18,13 @@ export function setOwnerStack(error: Error, stack: string | null) {
ownerStacks.set(error, stack) ownerStacks.set(error, stack)
} }
export function getReactStitchedError(err: unknown): Error { export function coerceError(value: unknown): Error {
const newError = isError(err) return isError(value) ? value : new Error('' + value)
? err }
: // TODO: stringify thrown value
new Error('Thrown value was ignored. This is a bug in Next.js.')
export function setOwnerStackIfAvailable(error: Error): void {
// React 18 and prod does not have `captureOwnerStack` // React 18 and prod does not have `captureOwnerStack`
if ('captureOwnerStack' in React) { if ('captureOwnerStack' in React) {
// TODO: Hoist these to callsites to ensure we set the correct Owner Stack. setOwnerStack(error, React.captureOwnerStack())
setOwnerStack(newError, React.captureOwnerStack())
} }
return newError
} }
...@@ -3,7 +3,7 @@ import { isNextRouterError } from '../is-next-router-error' ...@@ -3,7 +3,7 @@ import { isNextRouterError } from '../is-next-router-error'
import { formatConsoleArgs, parseConsoleArgs } from '../../lib/console' import { formatConsoleArgs, parseConsoleArgs } from '../../lib/console'
import isError from '../../../lib/is-error' import isError from '../../../lib/is-error'
import { createConsoleError } from './console-error' import { createConsoleError } from './console-error'
import { getReactStitchedError } from '../errors/stitched-error' import { coerceError, setOwnerStackIfAvailable } from '../errors/stitched-error'
const queueMicroTask = const queueMicroTask =
globalThis.queueMicrotask || ((cb: () => void) => Promise.resolve().then(cb)) globalThis.queueMicrotask || ((cb: () => void) => Promise.resolve().then(cb))
...@@ -29,7 +29,7 @@ export function handleConsoleError( ...@@ -29,7 +29,7 @@ export function handleConsoleError(
environmentName environmentName
) )
} }
error = getReactStitchedError(error) setOwnerStackIfAvailable(error)
errorQueue.push(error) errorQueue.push(error)
for (const handler of errorHandlers) { for (const handler of errorHandlers) {
...@@ -41,17 +41,7 @@ export function handleConsoleError( ...@@ -41,17 +41,7 @@ export function handleConsoleError(
} }
} }
export function handleClientError(originError: unknown) { export function handleClientError(error: Error) {
let error: Error
if (isError(originError)) {
error = originError
} else {
// If it's not an error, format the args into an error
const formattedErrorMessage = originError + ''
error = new Error(formattedErrorMessage)
}
error = getReactStitchedError(error)
errorQueue.push(error) errorQueue.push(error)
for (const handler of errorHandlers) { for (const handler of errorHandlers) {
// Delayed the error being passed to React Dev Overlay, // Delayed the error being passed to React Dev Overlay,
...@@ -91,28 +81,30 @@ export function useErrorHandler( ...@@ -91,28 +81,30 @@ export function useErrorHandler(
} }
function onUnhandledError(event: WindowEventMap['error']): void | boolean { function onUnhandledError(event: WindowEventMap['error']): void | boolean {
if (isNextRouterError(event.error)) { const thrownValue: unknown = event.error
if (isNextRouterError(thrownValue)) {
event.preventDefault() event.preventDefault()
return false return false
} }
// When there's an error property present, we log the error to error overlay. // When there's an error property present, we log the error to error overlay.
// Otherwise we don't do anything as it's not logging in the console either. // Otherwise we don't do anything as it's not logging in the console either.
if (event.error) { if (thrownValue) {
handleClientError(event.error) const error = coerceError(thrownValue)
setOwnerStackIfAvailable(error)
handleClientError(error)
} }
} }
function onUnhandledRejection(ev: WindowEventMap['unhandledrejection']): void { function onUnhandledRejection(ev: WindowEventMap['unhandledrejection']): void {
const reason = ev?.reason const reason: unknown = ev?.reason
if (isNextRouterError(reason)) { if (isNextRouterError(reason)) {
ev.preventDefault() ev.preventDefault()
return return
} }
let error = reason const error = coerceError(reason)
if (error && !isError(error)) { setOwnerStackIfAvailable(error)
error = new Error(error + '')
}
rejectionQueue.push(error) rejectionQueue.push(error)
for (const handler of rejectionHandlers) { for (const handler of rejectionHandlers) {
......
...@@ -52,7 +52,7 @@ function ReplaySsrOnlyErrors({ ...@@ -52,7 +52,7 @@ function ReplaySsrOnlyErrors({
// eslint-disable-next-line react-hooks/rules-of-hooks // eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => { useEffect(() => {
if (ssrError !== null) { if (ssrError !== null) {
// TODO(veil): Produces wrong Owner Stack // TODO(veil): Include original Owner Stack (NDX-905)
// TODO(veil): Mark as recoverable error // TODO(veil): Mark as recoverable error
// TODO(veil): console.error // TODO(veil): console.error
handleClientError(ssrError) handleClientError(ssrError)
......
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
import type { ErrorInfo } from 'react' import type { ErrorInfo } from 'react'
import { import {
getReactStitchedError, setOwnerStackIfAvailable,
setComponentStack, setComponentStack,
coerceError,
} from '../components/errors/stitched-error' } from '../components/errors/stitched-error'
import { handleClientError } from '../components/errors/use-error-handler' import { handleClientError } from '../components/errors/use-error-handler'
import { isNextRouterError } from '../components/is-next-router-error' import { isNextRouterError } from '../components/is-next-router-error'
...@@ -16,7 +17,7 @@ import { ...@@ -16,7 +17,7 @@ import {
} from '../components/error-boundary' } from '../components/error-boundary'
export function onCaughtError( export function onCaughtError(
err: unknown, thrownValue: unknown,
errorInfo: ErrorInfo & { errorBoundary?: React.Component } errorInfo: ErrorInfo & { errorBoundary?: React.Component }
) { ) {
const errorBoundaryComponent = errorInfo.errorBoundary?.constructor const errorBoundaryComponent = errorInfo.errorBoundary?.constructor
...@@ -41,11 +42,11 @@ export function onCaughtError( ...@@ -41,11 +42,11 @@ export function onCaughtError(
// We don't consider errors caught unless they're caught by an explicit error // We don't consider errors caught unless they're caught by an explicit error
// boundary. The built-in ones are considered implicit. // boundary. The built-in ones are considered implicit.
// This mimics how the same app would behave without Next.js. // This mimics how the same app would behave without Next.js.
return onUncaughtError(err, errorInfo) return onUncaughtError(thrownValue, errorInfo)
} }
// Skip certain custom errors which are not expected to be reported on client // Skip certain custom errors which are not expected to be reported on client
if (isBailoutToCSRError(err) || isNextRouterError(err)) return if (isBailoutToCSRError(thrownValue) || isNextRouterError(thrownValue)) return
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const errorBoundaryName = const errorBoundaryName =
...@@ -72,37 +73,43 @@ export function onCaughtError( ...@@ -72,37 +73,43 @@ export function onCaughtError(
const errorLocation = `${componentErrorMessage} ${errorBoundaryMessage}` const errorLocation = `${componentErrorMessage} ${errorBoundaryMessage}`
const stitchedError = getReactStitchedError(err) const error = coerceError(thrownValue)
setOwnerStackIfAvailable(error)
// TODO: change to passing down errorInfo later // TODO: change to passing down errorInfo later
// In development mode, pass along the component stack to the error // In development mode, pass along the component stack to the error
if (errorInfo.componentStack) { if (errorInfo.componentStack) {
setComponentStack(stitchedError, errorInfo.componentStack) setComponentStack(error, errorInfo.componentStack)
} }
// Log and report the error with location but without modifying the error stack // Log and report the error with location but without modifying the error stack
originConsoleError('%o\n\n%s', err, errorLocation) originConsoleError('%o\n\n%s', thrownValue, errorLocation)
handleClientError(stitchedError) handleClientError(error)
} else { } else {
originConsoleError(err) originConsoleError(thrownValue)
} }
} }
export function onUncaughtError(err: unknown, errorInfo: React.ErrorInfo) { export function onUncaughtError(
thrownValue: unknown,
errorInfo: React.ErrorInfo
) {
// Skip certain custom errors which are not expected to be reported on client // Skip certain custom errors which are not expected to be reported on client
if (isBailoutToCSRError(err) || isNextRouterError(err)) return if (isBailoutToCSRError(thrownValue) || isNextRouterError(thrownValue)) return
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const stitchedError = getReactStitchedError(err) const error = coerceError(thrownValue)
setOwnerStackIfAvailable(error)
// TODO: change to passing down errorInfo later // TODO: change to passing down errorInfo later
// In development mode, pass along the component stack to the error // In development mode, pass along the component stack to the error
if (errorInfo.componentStack) { if (errorInfo.componentStack) {
setComponentStack(stitchedError, errorInfo.componentStack) setComponentStack(error, errorInfo.componentStack)
} }
// TODO: Add an adendum to the overlay telling people about custom error boundaries. // TODO: Add an adendum to the overlay telling people about custom error boundaries.
reportGlobalError(stitchedError) reportGlobalError(error)
} else { } else {
reportGlobalError(err) reportGlobalError(thrownValue)
} }
} }
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
import type { HydrationOptions } from 'react-dom/client' import type { HydrationOptions } from 'react-dom/client'
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { reportGlobalError } from './report-global-error'
import { import {
getReactStitchedError, setOwnerStackIfAvailable,
setComponentStack, setComponentStack,
coerceError,
} from '../components/errors/stitched-error' } from '../components/errors/stitched-error'
import isError from '../../lib/is-error' import isError from '../../lib/is-error'
import { reportGlobalError } from './report-global-error'
export const onRecoverableError: HydrationOptions['onRecoverableError'] = ( export const onRecoverableError: HydrationOptions['onRecoverableError'] = (
error, error,
...@@ -15,12 +16,15 @@ export const onRecoverableError: HydrationOptions['onRecoverableError'] = ( ...@@ -15,12 +16,15 @@ export const onRecoverableError: HydrationOptions['onRecoverableError'] = (
) => { ) => {
// x-ref: https://github.com/facebook/react/pull/28736 // x-ref: https://github.com/facebook/react/pull/28736
const cause = isError(error) && 'cause' in error ? error.cause : error const cause = isError(error) && 'cause' in error ? error.cause : error
const stitchedError = getReactStitchedError(cause)
if (process.env.NODE_ENV === 'development' && errorInfo.componentStack) {
setComponentStack(stitchedError, errorInfo.componentStack)
}
// Skip certain custom errors which are not expected to be reported on client // Skip certain custom errors which are not expected to be reported on client
if (isBailoutToCSRError(cause)) return if (isBailoutToCSRError(cause)) return
reportGlobalError(stitchedError) const causeError = coerceError(cause)
setOwnerStackIfAvailable(causeError)
if (process.env.NODE_ENV === 'development' && errorInfo.componentStack) {
setComponentStack(causeError, errorInfo.componentStack)
}
reportGlobalError(causeError)
} }
...@@ -9,7 +9,7 @@ describe('ssr-only-error', () => { ...@@ -9,7 +9,7 @@ describe('ssr-only-error', () => {
it('should show ssr only error in error overlay', async () => { it('should show ssr only error in error overlay', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
// TODO(veil): Missing Owner Stack // TODO(veil): Missing Owner Stack (NDX-905)
await expect(browser).toDisplayCollapsedRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"description": "Error: SSR only error", "description": "Error: SSR only error",
......
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