-
Notifications
You must be signed in to change notification settings - Fork 171
⚗ 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
Open
BeltranBulbarellaDD
wants to merge
14
commits into
main
Choose a base branch
from
beltran.bulbarella/nextjs_pages__router_error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fda52cb
Add error boundary to Rum-Nextjs
BeltranBulbarellaDD 48f6769
Add e2e tests
BeltranBulbarellaDD 3cf211b
Disallow side effect from React, CI fixes
BeltranBulbarellaDD e0d3dee
fix rum-react version
BeltranBulbarellaDD cd405c3
Remove import
BeltranBulbarellaDD f4aedea
Add error boundary to Rum-Nextjs
BeltranBulbarellaDD 676d170
build
BeltranBulbarellaDD bb905f7
add skill
BeltranBulbarellaDD c7e7ca7
Fix tests
BeltranBulbarellaDD 6d7f023
format
BeltranBulbarellaDD 818eab3
Remove internal file, add firefox timeout, update nextjs dependencies.
BeltranBulbarellaDD 58eb95b
Remove unused file
BeltranBulbarellaDD 3cb098b
Skip Firefox test on pages router error, add internal types for error…
BeltranBulbarellaDD 9254f01
Prevent duplicate error name
BeltranBulbarellaDD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/rum-nextjs/src/domain/error/errorBoundary.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import React, { act } from 'react' | ||
|
|
||
| import { | ||
| disableJasmineUncaughtExceptionTracking, | ||
| ignoreConsoleLogs, | ||
| registerCleanupTask, | ||
| } from '@datadog/browser-core/test' | ||
| import type { RumInitConfiguration, RumPublicApi } from '@datadog/browser-rum-core' | ||
| import { appendComponent } from '../../../../rum-react/test/appendComponent' | ||
| import { initReactOldBrowsersSupport } from '../../../../rum-react/test/reactOldBrowsersSupport' | ||
| import { nextjsPlugin, resetNextjsPlugin } from '../nextjsPlugin' | ||
| import type { ErrorBoundaryFallback } from './errorBoundary' | ||
| import { ErrorBoundary } from './errorBoundary' | ||
|
|
||
| type FallbackFunctionComponent = Extract<ErrorBoundaryFallback, (...args: any[]) => any> | ||
|
|
||
| function initializeNextjsPlugin() { | ||
| const addErrorSpy = jasmine.createSpy() | ||
| const publicApi = { startView: jasmine.createSpy() } as unknown as RumPublicApi | ||
| const plugin = nextjsPlugin() | ||
| plugin.onInit({ publicApi, initConfiguration: {} as RumInitConfiguration }) | ||
| plugin.onRumStart({ addError: addErrorSpy }) | ||
| return { addErrorSpy } | ||
| } | ||
|
|
||
| describe('NextjsErrorBoundary', () => { | ||
| beforeEach(() => { | ||
| ignoreConsoleLogs('error', 'Error: error') | ||
| disableJasmineUncaughtExceptionTracking() | ||
| initReactOldBrowsersSupport() | ||
| resetNextjsPlugin() | ||
| registerCleanupTask(() => resetNextjsPlugin()) | ||
| }) | ||
|
|
||
| it('renders children', () => { | ||
| const container = appendComponent(<ErrorBoundary fallback={() => null}>bar</ErrorBoundary>) | ||
| expect(container.innerHTML).toBe('bar') | ||
| }) | ||
|
|
||
| it('renders the fallback when an error occurs', () => { | ||
| const fallbackSpy = jasmine.createSpy<FallbackFunctionComponent>().and.returnValue('fallback') | ||
| const ComponentSpy = jasmine.createSpy().and.throwError(new Error('error')) | ||
| const container = appendComponent( | ||
| <ErrorBoundary fallback={fallbackSpy}> | ||
| <ComponentSpy /> | ||
| </ErrorBoundary> | ||
| ) | ||
|
|
||
| expect(fallbackSpy).toHaveBeenCalled() | ||
| fallbackSpy.calls.all().forEach(({ args }) => { | ||
| expect(args[0]).toEqual({ | ||
| error: new Error('error'), | ||
| resetError: jasmine.any(Function), | ||
| }) | ||
| }) | ||
| expect(container.innerHTML).toBe('fallback') | ||
| }) | ||
|
|
||
| it('resets the error when resetError is called', () => { | ||
| const fallbackSpy = jasmine.createSpy<FallbackFunctionComponent>().and.returnValue('fallback') | ||
| const ComponentSpy = jasmine.createSpy().and.throwError(new Error('error')) | ||
| const container = appendComponent( | ||
| <ErrorBoundary fallback={fallbackSpy}> | ||
| <ComponentSpy /> | ||
| </ErrorBoundary> | ||
| ) | ||
|
|
||
| ComponentSpy.and.returnValue('bar') | ||
|
|
||
| const { resetError } = fallbackSpy.calls.mostRecent().args[0] | ||
| act(() => { | ||
| resetError() | ||
| }) | ||
|
|
||
| expect(container.innerHTML).toBe('bar') | ||
| }) | ||
|
|
||
| it('reports the error through addNextjsError', () => { | ||
| const { addErrorSpy } = initializeNextjsPlugin() | ||
| 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'), | ||
| }) | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from '../src/entries/internal' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ export const config: Config = { | |
| name: 'nextjs app router', | ||
| stdout: 'pipe' as const, | ||
| cwd: path.join(__dirname, '../apps/nextjs'), | ||
| command: isLocal ? 'yarn dev' : 'yarn start', | ||
| command: 'yarn start', | ||
|
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. This is for 2 reasons:
|
||
| wait: { | ||
| stdout: /- Local:\s+http:\/\/localhost:(?<nextjs_app_router_port>\d+)/, | ||
| }, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since all these tests are the same for rum-react (except for the last one) should I remove them?