Skip to content
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

add the errorContext prop to the ErrorBoundary component #3258

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

eloytoro
Copy link

@eloytoro eloytoro commented Jan 7, 2025

Motivation

This is a simple, yet useful, change. Giving customers the ability to pass an errorContext prop to the ErrorBoundary component could improve the triaging experiencing of the error. Take this for example:

function List() {
  return (
    <div>
      <ErrorBoundary errorContext={{ item: 0 }}>
        <ListItem id={0} />
      </ErrorBoundary>
      <ErrorBoundary errorContext={{ item: 1 }}>
        <ListItem id={1} />
      </ErrorBoundary>
    </div>
  );
}

In the component above both ListItem elements are capable of producing an error, and when doing so they would both have the same componentStack reported by react, but since the item prop was added to the error’s context then in Datadog it would be possible to determine which component threw the error.

Changes

  • Take errorContext as a prop by the ErrorBoundary
  • Add the context to the payload sent by the SDK

Testing

  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

@eloytoro eloytoro requested a review from a team as a code owner January 7, 2025 10:31
@@ -2,12 +2,12 @@ import type { ErrorInfo } from 'react'
import type { ErrorWithCause } from '@datadog/browser-core'
import { onReactPluginInit } from '../reactPlugin'

export function addReactError(error: Error, info: ErrorInfo) {
export function addReactError(error: Error, context: object | undefined, info: ErrorInfo) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 suggestion: ‏add the context parameter at the end, so we can make it optional + avoid a breaking change

Suggested change
export function addReactError(error: Error, context: object | undefined, info: ErrorInfo) {
export function addReactError(error: Error, info: ErrorInfo, context?: object) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants