-
Notifications
You must be signed in to change notification settings - Fork 179
⚗ NextJS- add nextjs error boundary component #4352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fda52cb
48f6769
3cf211b
e0d3dee
cd405c3
f4aedea
676d170
bb905f7
c7e7ca7
6d7f023
818eab3
58eb95b
3cb098b
9254f01
aaf0066
7807ff3
e81b850
5572aa3
7a91c27
b4185ae
6aa5690
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import React from 'react' | ||
|
|
||
| import { disableJasmineUncaughtExceptionTracking, ignoreConsoleLogs } from '@datadog/browser-core/test' | ||
| import { appendComponent } from '../../../../rum-react/test/appendComponent' | ||
| import { initReactOldBrowsersSupport } from '../../../../rum-react/test/reactOldBrowsersSupport' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test imports
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Maybe we can expose them as shared utility, that would be better. |
||
| import { initializeNextjsPlugin } from '../../../test/initializeNextjsPlugin' | ||
| import { ErrorBoundary } from './errorBoundary' | ||
|
|
||
| // Component behavior (renders children, fallback, resetError) is tested via createErrorBoundary | ||
| // in packages/rum-react/src/domain/error/errorBoundary.spec.tsx | ||
|
|
||
| describe('NextjsErrorBoundary', () => { | ||
| it('reports the error through addNextjsError', () => { | ||
| ignoreConsoleLogs('error', 'Error: error') | ||
| disableJasmineUncaughtExceptionTracking() | ||
| initReactOldBrowsersSupport() | ||
|
|
||
| const addErrorSpy = jasmine.createSpy() | ||
| initializeNextjsPlugin({ addError: addErrorSpy }) | ||
| const originalError = new Error('error') | ||
| const ComponentSpy = jasmine.createSpy().and.throwError(originalError) | ||
| ;(ComponentSpy as any).displayName = 'ComponentSpy' | ||
|
|
||
| appendComponent( | ||
| <ErrorBoundary fallback={() => null}> | ||
| <ComponentSpy /> | ||
| </ErrorBoundary> | ||
| ) | ||
|
|
||
| expect(addErrorSpy).toHaveBeenCalledOnceWith( | ||
| jasmine.objectContaining({ | ||
| error: originalError, | ||
| handlingStack: jasmine.any(String), | ||
| startClocks: jasmine.any(Object), | ||
| context: jasmine.objectContaining({ | ||
| framework: 'nextjs', | ||
| }), | ||
| componentStack: jasmine.stringContaining('ComponentSpy'), | ||
| }) | ||
| ) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| 'use client' | ||
| import { createErrorBoundary } from '@datadog/browser-rum-react/internal' | ||
| export type { ErrorBoundaryFallback, ErrorBoundaryProps } from '@datadog/browser-rum-react/internal' | ||
| import { addNextjsError } from './addNextjsError' | ||
|
|
||
| /** | ||
| * ErrorBoundary component to report React errors to Datadog using the Next.js error context. | ||
| * | ||
| * For more advanced error handling, you can use the {@link addNextjsError} function. | ||
| * | ||
| * @category Error | ||
| * @example | ||
| * ```ts | ||
| * import { ErrorBoundary } from '@datadog/browser-rum-nextjs' | ||
| * | ||
| * <ErrorBoundary fallback={() => null}> | ||
| * <Component /> | ||
| * </ErrorBoundary> | ||
| * ``` | ||
| */ | ||
| // eslint-disable-next-line local-rules/disallow-side-effects | ||
| export const ErrorBoundary = createErrorBoundary(addNextjsError) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { ErrorBoundary } from '@datadog/browser-rum-nextjs' | ||
| import type { ErrorBoundaryFallback } from '@datadog/browser-rum-nextjs' | ||
| import { useState } from 'react' | ||
| import Link from 'next/link' | ||
|
|
||
| function ErrorThrower() { | ||
| const [shouldThrow, setShouldThrow] = useState(false) | ||
| if (shouldThrow) throw new Error('Pages Router error from NextjsErrorBoundary') | ||
| return ( | ||
| <button data-testid="trigger-error" onClick={() => setShouldThrow(true)}> | ||
| Trigger Error | ||
| </button> | ||
| ) | ||
| } | ||
|
|
||
| const ErrorFallback: ErrorBoundaryFallback = ({ error, resetError }) => ( | ||
| <div data-testid="error-boundary"> | ||
| <p>{error.message}</p> | ||
| <button data-testid="reset-error" onClick={resetError}> | ||
| Reset | ||
| </button> | ||
| </div> | ||
| ) | ||
|
|
||
| export default function ErrorTestPage() { | ||
| return ( | ||
| <div> | ||
| <Link href="/pages-router">← Back to Home</Link> | ||
| <h1>Error Test</h1> | ||
| <ErrorBoundary fallback={ErrorFallback}> | ||
| <ErrorThrower /> | ||
| </ErrorBoundary> | ||
| </div> | ||
| ) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.