@@ -50,7 +50,7 @@ import { parseSampleRate } from './utils/parseSampleRate';
5050import { prepareEvent } from './utils/prepareEvent' ;
5151import { makePromiseBuffer , type PromiseBuffer , SENTRY_BUFFER_FULL_ERROR } from './utils/promisebuffer' ;
5252import { safeMathRandom } from './utils/randomSafeContext' ;
53- import { safeCallback } from './utils/safeCallback' ;
53+ import { CALLBACK_ERROR , safeCallback } from './utils/safeCallback' ;
5454import { reparentChildSpans , shouldIgnoreSpan } from './utils/should-ignore-span' ;
5555import { safeUnref } from './utils/timer' ;
5656import { convertSpanJsonToTransactionEvent , convertTransactionEventToSpanJson } from './utils/transactionEvent' ;
@@ -1522,6 +1522,14 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
15221522 const parsedSampleRate = typeof sampleRate === 'undefined' ? undefined : parseSampleRate ( sampleRate ) ;
15231523 const dataCategory = getDataCategoryByType ( event . type ) ;
15241524
1525+ const recordDroppedEvent = ( reason : EventDropReason ) : void => {
1526+ this . recordDroppedEvent ( reason , dataCategory ) ;
1527+ if ( isTransaction ) {
1528+ // the transaction itself counts as one span, plus all the child spans that are added
1529+ this . recordDroppedEvent ( reason , 'span' , 1 + ( event . spans || [ ] ) . length ) ;
1530+ }
1531+ } ;
1532+
15251533 return this . _prepareEvent ( event , hint , currentScope , isolationScope )
15261534 . then ( prepared => {
15271535 if ( prepared === null ) {
@@ -1539,13 +1547,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
15391547 } )
15401548 . then ( processedEvent => {
15411549 if ( processedEvent === null ) {
1542- this . recordDroppedEvent ( 'before_send' , dataCategory ) ;
1543- if ( isTransaction ) {
1544- const spans = event . spans || [ ] ;
1545- // the transaction itself counts as one span, plus all the child spans that are added
1546- const spanCount = 1 + spans . length ;
1547- this . recordDroppedEvent ( 'before_send' , 'span' , spanCount ) ;
1548- }
1550+ recordDroppedEvent ( 'before_send' ) ;
15491551 throw _makeDoNotSendEventError ( `${ beforeSendLabel } returned \`null\`, will not send event.` ) ;
15501552 }
15511553
@@ -1587,6 +1589,11 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
15871589 return processedEvent ;
15881590 } )
15891591 . then ( null , reason => {
1592+ if ( reason === CALLBACK_ERROR ) {
1593+ recordDroppedEvent ( 'callback_error' ) ;
1594+ throw _makeDoNotSendEventError ( 'A user callback threw an error, will not send event.' ) ;
1595+ }
1596+
15901597 if ( _isDoNotSendEventError ( reason ) || _isInternalError ( reason ) ) {
15911598 throw reason ;
15921599 }
@@ -1702,17 +1709,13 @@ function _validateBeforeSendResult(
17021709) : PromiseLike < Event | null > | Event | null {
17031710 const invalidValueError = `${ beforeSendLabel } must return \`null\` or a valid event.` ;
17041711 if ( isThenable ( beforeSendResult ) ) {
1705- return beforeSendResult . then (
1706- event => {
1707- if ( ! isPlainObject ( event ) && event !== null ) {
1708- throw _makeInternalError ( invalidValueError ) ;
1709- }
1710- return event ;
1711- } ,
1712- e => {
1713- throw _makeInternalError ( `${ beforeSendLabel } rejected with ${ e } ` ) ;
1714- } ,
1715- ) ;
1712+ // A rejection can only be `CALLBACK_ERROR` here, as `safeCallback` already handled the user callback rejecting
1713+ return beforeSendResult . then ( event => {
1714+ if ( ! isPlainObject ( event ) && event !== null ) {
1715+ throw _makeInternalError ( invalidValueError ) ;
1716+ }
1717+ return event ;
1718+ } ) ;
17161719 } else if ( ! isPlainObject ( beforeSendResult ) && beforeSendResult !== null ) {
17171720 throw _makeInternalError ( invalidValueError ) ;
17181721 }
@@ -1743,7 +1746,9 @@ function processBeforeSend(
17431746 return safeCallback (
17441747 DEBUG_BUILD ? 'The `beforeSend` callback threw an error, dropping the event:' : '' ,
17451748 ( ) => beforeSend ( errorEvent , hint ) ,
1746- ( ) => null ,
1749+ ( ) => {
1750+ throw CALLBACK_ERROR ;
1751+ } ,
17471752 ) ;
17481753 }
17491754
@@ -1818,7 +1823,9 @@ function processBeforeSend(
18181823 return safeCallback (
18191824 DEBUG_BUILD ? 'The `beforeSendTransaction` callback threw an error, dropping the event:' : '' ,
18201825 ( ) => beforeSendTransaction ( processedEvent as TransactionEvent , hint ) ,
1821- ( ) => null ,
1826+ ( ) => {
1827+ throw CALLBACK_ERROR ;
1828+ } ,
18221829 ) ;
18231830 }
18241831 }
0 commit comments