Skip to content

The captureException method inside the withScope method didn't work #17437

@Chorer

Description

@Chorer

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

6.19.7

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

No response

Steps to Reproduce

  1. use ErrorBoundary in React project:
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
 // ......
  componentDidCatch(error: Error, errorInfo: any) {
    if (window.Sentry) {
      window.Sentry.withScope(scope => {
        Object.keys(errorInfo).forEach(key => {
          scope.setExtra(key, errorInfo[key])
        })
        window.Sentry.captureException(error)
      })
    }
  }
  // ......
}
export default ErrorBoundary
  1. throw an error in the example component:
export const Example = () => {
    useEffect(() => {
        throw new Error('this is the error')
    })
     return <div>example</div>
}

Expected Result

ErrorBoundary catch the error and report it to the sentry platform with window.Sentry.captureException

Actual Result

window.Sentry.captureException didn't work and none of sentry requests sent. But when I added another window.Sentry.captureException outside the window.Sentry.withScope, it worked. Both of the captureException be executed.

componentDidCatch(error: Error, errorInfo: any) {
    if (window.Sentry) {
      window.Sentry.withScope(scope => {
        Object.keys(errorInfo).forEach(key => {
          scope.setExtra(key, errorInfo[key])
        })
        window.Sentry.captureException(error)
      })
     window.Sentry.captureException(new Error('error outside'))
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions