1- import * as Sentry from '@sentry/browser' ;
1+ import { captureException , ReportDialogOptions , Scope , showReportDialog , withScope } from '@sentry/browser' ;
22import * as hoistNonReactStatic from 'hoist-non-react-statics' ;
33import * as React from 'react' ;
44
@@ -18,7 +18,7 @@ export type ErrorBoundaryProps = {
1818 * Options to be passed into the Sentry report dialog.
1919 * No-op if {@link showDialog} is false.
2020 */
21- dialogOptions ?: Sentry . ReportDialogOptions ;
21+ dialogOptions ?: ReportDialogOptions ;
2222 // tslint:disable no-null-undefined-union
2323 /**
2424 * A fallback component that gets rendered when the error boundary encounters an error.
@@ -38,6 +38,8 @@ export type ErrorBoundaryProps = {
3838 onReset ?( error : Error | null , componentStack : string | null , eventId : string | null ) : void ;
3939 /** Called on componentWillUnmount() */
4040 onUnmount ?( error : Error | null , componentStack : string | null , eventId : string | null ) : void ;
41+ /** Called before the error is captured by Sentry, allows for you to add tags or context using the scope */
42+ beforeCapture ?( scope : Scope , error : Error | null , componentStack : string | null ) : void ;
4143} ;
4244
4345type ErrorBoundaryState = {
@@ -60,18 +62,24 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
6062 public state : ErrorBoundaryState = INITIAL_STATE ;
6163
6264 public componentDidCatch ( error : Error , { componentStack } : React . ErrorInfo ) : void {
63- const eventId = Sentry . captureException ( error , { contexts : { react : { componentStack } } } ) ;
64- const { onError, showDialog, dialogOptions } = this . props ;
65- if ( onError ) {
66- onError ( error , componentStack , eventId ) ;
67- }
68- if ( showDialog ) {
69- Sentry . showReportDialog ( { ...dialogOptions , eventId } ) ;
70- }
65+ const { beforeCapture, onError, showDialog, dialogOptions } = this . props ;
66+
67+ withScope ( scope => {
68+ if ( beforeCapture ) {
69+ beforeCapture ( scope , error , componentStack ) ;
70+ }
71+ const eventId = captureException ( error , { contexts : { react : { componentStack } } } ) ;
72+ if ( onError ) {
73+ onError ( error , componentStack , eventId ) ;
74+ }
75+ if ( showDialog ) {
76+ showReportDialog ( { ...dialogOptions , eventId } ) ;
77+ }
7178
72- // componentDidCatch is used over getDerivedStateFromError
73- // so that componentStack is accessible through state.
74- this . setState ( { error, componentStack, eventId } ) ;
79+ // componentDidCatch is used over getDerivedStateFromError
80+ // so that componentStack is accessible through state.
81+ this . setState ( { error, componentStack, eventId } ) ;
82+ } ) ;
7583 }
7684
7785 public componentDidMount ( ) : void {
0 commit comments