@@ -5,17 +5,18 @@ import {
55 DataCategory ,
66 DsnComponents ,
77 Envelope ,
8+ ErrorEvent ,
89 Event ,
910 EventDropReason ,
1011 EventHint ,
11- EventType ,
1212 Integration ,
1313 IntegrationClass ,
1414 Outcome ,
1515 Session ,
1616 SessionAggregates ,
1717 Severity ,
1818 SeverityLevel ,
19+ TransactionEvent ,
1920 Transport ,
2021} from '@sentry/types' ;
2122import {
@@ -43,8 +44,6 @@ import { IntegrationIndex, setupIntegrations } from './integration';
4344import { Scope } from './scope' ;
4445import { updateSession } from './session' ;
4546
46- type BeforeSendProcessorMethod = 'beforeSend' | 'beforeSendTransaction' ;
47-
4847const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured." ;
4948
5049/**
@@ -633,13 +632,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
633632 const isTransaction = event . type === 'transaction' ;
634633 const isError = ! event . type ;
635634
636- const beforeSendProcessorMap : Map < EventType | undefined , BeforeSendProcessorMethod | undefined > = new Map ( [
637- [ undefined , 'beforeSend' ] ,
638- [ 'transaction' , 'beforeSendTransaction' ] ,
639- ] ) ;
640-
641- const beforeSendProcessorName = beforeSendProcessorMap . get ( event . type ) ;
642- const beforeSendProcessor = beforeSendProcessorName ? options [ beforeSendProcessorName ] : undefined ;
635+ const beforeSendProcessorName = getBeforeSendMethodName ( event ) ;
643636
644637 // 1.0 === 100% events are sent
645638 // 0.0 === 0% events are sent
@@ -662,12 +655,12 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
662655 }
663656
664657 const isInternalException = hint . data && ( hint . data as { __sentry__ : boolean } ) . __sentry__ === true ;
665- if ( isInternalException || ! beforeSendProcessor ) {
658+ if ( isInternalException ) {
666659 return prepared ;
667660 }
668661
669- const beforeSendResult = beforeSendProcessor ( prepared , hint ) ;
670- return _validateBeforeSendResult ( beforeSendResult , beforeSendProcessorName as BeforeSendProcessorMethod ) ;
662+ const beforeSendResult = processBeforeSend ( options , prepared , hint ) ;
663+ return _validateBeforeSendResult ( beforeSendResult , beforeSendProcessorName ) ;
671664 } )
672665 . then ( processedEvent => {
673666 if ( processedEvent === null ) {
@@ -789,7 +782,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
789782 */
790783function _validateBeforeSendResult (
791784 beforeSendResult : PromiseLike < Event | null > | Event | null ,
792- beforeSendProcessorName : 'beforeSend' | 'beforeSendTransaction' ,
785+ beforeSendProcessorName : string ,
793786) : PromiseLike < Event | null > | Event | null {
794787 const invalidValueError = `\`${ beforeSendProcessorName } \` must return \`null\` or a valid event.` ;
795788 if ( isThenable ( beforeSendResult ) ) {
@@ -809,3 +802,46 @@ function _validateBeforeSendResult(
809802 }
810803 return beforeSendResult ;
811804}
805+
806+ /**
807+ * Process the matching `beforeSendXXX` callback.
808+ */
809+ function processBeforeSend < T extends Event > (
810+ options : ClientOptions ,
811+ event : T ,
812+ hint : EventHint ,
813+ ) : PromiseLike < T | null > | T | null {
814+ const { beforeSend, beforeSendTransaction } = options ;
815+
816+ if ( isErrorEvent ( event ) && beforeSend ) {
817+ return beforeSend ( event , hint ) as PromiseLike < T | null > | T | null ;
818+ }
819+
820+ if ( isTransactionEvent ( event ) && beforeSendTransaction ) {
821+ return beforeSendTransaction ( event , hint ) as PromiseLike < T | null > | T | null ;
822+ }
823+
824+ return event ;
825+ }
826+
827+ /** Get the name of the before send processor for logging purposes. */
828+ function getBeforeSendMethodName < T extends Event > ( event : T ) : string {
829+ if ( isErrorEvent ( event ) ) {
830+ return 'beforeSend' ;
831+ }
832+
833+ if ( isTransactionEvent ( event ) ) {
834+ return 'beforeSendTransaction' ;
835+ }
836+
837+ // This shouldn't happen, but if it does, we need to return a string
838+ return 'unknown' ;
839+ }
840+
841+ function isErrorEvent ( event : Event ) : event is ErrorEvent {
842+ return event . type === undefined ;
843+ }
844+
845+ function isTransactionEvent ( event : Event ) : event is TransactionEvent {
846+ return event . type === 'transaction' ;
847+ }
0 commit comments