1
- import * as Sentry from '@sentry/browser' ;
1
+ import { captureException , ReportDialogOptions , Scope , showReportDialog , withScope } from '@sentry/browser' ;
2
2
import * as hoistNonReactStatic from 'hoist-non-react-statics' ;
3
3
import * as React from 'react' ;
4
4
@@ -18,7 +18,7 @@ export type ErrorBoundaryProps = {
18
18
* Options to be passed into the Sentry report dialog.
19
19
* No-op if {@link showDialog} is false.
20
20
*/
21
- dialogOptions ?: Sentry . ReportDialogOptions ;
21
+ dialogOptions ?: ReportDialogOptions ;
22
22
// tslint:disable no-null-undefined-union
23
23
/**
24
24
* A fallback component that gets rendered when the error boundary encounters an error.
@@ -38,6 +38,8 @@ export type ErrorBoundaryProps = {
38
38
onReset ?( error : Error | null , componentStack : string | null , eventId : string | null ) : void ;
39
39
/** Called on componentWillUnmount() */
40
40
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 ;
41
43
} ;
42
44
43
45
type ErrorBoundaryState = {
@@ -60,18 +62,24 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
60
62
public state : ErrorBoundaryState = INITIAL_STATE ;
61
63
62
64
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
+ }
71
78
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
+ } ) ;
75
83
}
76
84
77
85
public componentDidMount ( ) : void {
0 commit comments