11/* eslint-disable max-lines */ // TODO: We might want to split this file up
2- import { addGlobalEventProcessor , getCurrentHub , Scope , setContext } from '@sentry/core' ;
2+ import { addGlobalEventProcessor , captureException , getCurrentHub , Scope , setContext } from '@sentry/core' ;
33import { Breadcrumb , Client , Event } from '@sentry/types' ;
44import { addInstrumentationHandler , createEnvelope , logger } from '@sentry/utils' ;
55import debounce from 'lodash.debounce' ;
@@ -40,7 +40,6 @@ import type {
4040} from './types' ;
4141import { addEvent } from './util/addEvent' ;
4242import { addMemoryEntry } from './util/addMemoryEntry' ;
43- import { captureInternalException } from './util/captureInternalException' ;
4443import { createBreadcrumb } from './util/createBreadcrumb' ;
4544import { createPayload } from './util/createPayload' ;
4645import { createPerformanceSpans } from './util/createPerformanceSpans' ;
@@ -160,7 +159,7 @@ export class ReplayContainer implements ReplayContainerInterface {
160159
161160 // If there is no session, then something bad has happened - can't continue
162161 if ( ! this . session ) {
163- captureInternalException ( new Error ( 'Invalid session' ) ) ;
162+ this . handleException ( new Error ( 'No session found ' ) ) ;
164163 return ;
165164 }
166165
@@ -208,8 +207,7 @@ export class ReplayContainer implements ReplayContainerInterface {
208207 emit : this . handleRecordingEmit ,
209208 } ) ;
210209 } catch ( err ) {
211- __DEBUG_BUILD__ && logger . error ( '[Replay]' , err ) ;
212- captureInternalException ( err ) ;
210+ this . handleException ( err ) ;
213211 }
214212 }
215213
@@ -239,8 +237,7 @@ export class ReplayContainer implements ReplayContainerInterface {
239237 this . eventBuffer ?. destroy ( ) ;
240238 this . eventBuffer = null ;
241239 } catch ( err ) {
242- __DEBUG_BUILD__ && logger . error ( '[Replay]' , err ) ;
243- captureInternalException ( err ) ;
240+ this . handleException ( err ) ;
244241 }
245242 }
246243
@@ -257,8 +254,7 @@ export class ReplayContainer implements ReplayContainerInterface {
257254 this . _stopRecording = undefined ;
258255 }
259256 } catch ( err ) {
260- __DEBUG_BUILD__ && logger . error ( '[Replay]' , err ) ;
261- captureInternalException ( err ) ;
257+ this . handleException ( err ) ;
262258 }
263259 }
264260
@@ -273,14 +269,22 @@ export class ReplayContainer implements ReplayContainerInterface {
273269 this . startRecording ( ) ;
274270 }
275271
272+ /** A wrapper to conditionally capture exceptions. */
273+ handleException ( error : unknown ) : void {
274+ __DEBUG_BUILD__ && logger . error ( '[Replay]' , error ) ;
275+
276+ if ( this . options . _experiments && this . options . _experiments . captureExceptions ) {
277+ captureException ( error ) ;
278+ }
279+ }
280+
276281 /** for tests only */
277282 clearSession ( ) : void {
278283 try {
279284 deleteSession ( ) ;
280285 this . session = undefined ;
281286 } catch ( err ) {
282- __DEBUG_BUILD__ && logger . error ( '[Replay]' , err ) ;
283- captureInternalException ( err ) ;
287+ this . handleException ( err ) ;
284288 }
285289 }
286290
@@ -358,8 +362,7 @@ export class ReplayContainer implements ReplayContainerInterface {
358362 this . _hasInitializedCoreListeners = true ;
359363 }
360364 } catch ( err ) {
361- __DEBUG_BUILD__ && logger . error ( '[Replay]' , err ) ;
362- captureInternalException ( err ) ;
365+ this . handleException ( err ) ;
363366 }
364367
365368 // _performanceObserver //
@@ -387,8 +390,7 @@ export class ReplayContainer implements ReplayContainerInterface {
387390 this . _performanceObserver = null ;
388391 }
389392 } catch ( err ) {
390- __DEBUG_BUILD__ && logger . error ( '[Replay]' , err ) ;
391- captureInternalException ( err ) ;
393+ this . handleException ( err ) ;
392394 }
393395 }
394396
@@ -827,8 +829,7 @@ export class ReplayContainer implements ReplayContainerInterface {
827829 eventContext,
828830 } ) ;
829831 } catch ( err ) {
830- __DEBUG_BUILD__ && logger . error ( err ) ;
831- captureInternalException ( err ) ;
832+ this . handleException ( err ) ;
832833 }
833834 }
834835
@@ -1021,12 +1022,11 @@ export class ReplayContainer implements ReplayContainerInterface {
10211022 this . resetRetries ( ) ;
10221023 return true ;
10231024 } catch ( err ) {
1024- __DEBUG_BUILD__ && logger . error ( err ) ;
10251025 // Capture error for every failed replay
10261026 setContext ( 'Replays' , {
10271027 _retryCount : this . _retryCount ,
10281028 } ) ;
1029- captureInternalException ( err ) ;
1029+ this . handleException ( err ) ;
10301030
10311031 // If an error happened here, it's likely that uploading the attachment
10321032 // failed, we'll can retry with the same events payload
0 commit comments