11import type { Span , TransactionContext } from '@sentry/types' ;
2- import { dropUndefinedKeys , isThenable , logger , tracingContextFromHeaders } from '@sentry/utils' ;
2+ import { dropUndefinedKeys , logger , tracingContextFromHeaders } from '@sentry/utils' ;
33
44import { DEBUG_BUILD } from '../debug-build' ;
55import { getCurrentScope , withScope } from '../exports' ;
66import type { Hub } from '../hub' ;
77import { getCurrentHub } from '../hub' ;
8+ import { handleCallbackErrors } from '../utils/handleCallbackErrors' ;
89import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
910
1011/**
@@ -18,6 +19,8 @@ import { hasTracingEnabled } from '../utils/hasTracingEnabled';
1819 *
1920 * @internal
2021 * @private
22+ *
23+ * @deprecated Use `startSpan` instead.
2124 */
2225export function trace < T > (
2326 context : TransactionContext ,
@@ -37,43 +40,18 @@ export function trace<T>(
3740
3841 scope . setSpan ( activeSpan ) ;
3942
40- function finishAndSetSpan ( ) : void {
41- activeSpan && activeSpan . end ( ) ;
42- scope . setSpan ( parentSpan ) ;
43- }
44-
45- let maybePromiseResult : T ;
46- try {
47- maybePromiseResult = callback ( activeSpan ) ;
48- } catch ( e ) {
49- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
50- onError ( e , activeSpan ) ;
51- finishAndSetSpan ( ) ;
52- afterFinish ( ) ;
53- throw e ;
54- }
55-
56- if ( isThenable ( maybePromiseResult ) ) {
57- // @ts -expect-error - the isThenable check returns the "wrong" type here
58- return maybePromiseResult . then (
59- res => {
60- finishAndSetSpan ( ) ;
61- afterFinish ( ) ;
62- return res ;
63- } ,
64- e => {
65- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
66- onError ( e , activeSpan ) ;
67- finishAndSetSpan ( ) ;
68- afterFinish ( ) ;
69- throw e ;
70- } ,
71- ) ;
72- }
73-
74- finishAndSetSpan ( ) ;
75- afterFinish ( ) ;
76- return maybePromiseResult ;
43+ return handleCallbackErrors (
44+ ( ) => callback ( activeSpan ) ,
45+ error => {
46+ activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
47+ onError ( error , activeSpan ) ;
48+ } ,
49+ ( ) => {
50+ activeSpan && activeSpan . end ( ) ;
51+ scope . setSpan ( parentSpan ) ;
52+ afterFinish ( ) ;
53+ } ,
54+ ) ;
7755}
7856
7957/**
@@ -90,43 +68,23 @@ export function trace<T>(
9068export function startSpan < T > ( context : TransactionContext , callback : ( span : Span | undefined ) => T ) : T {
9169 const ctx = normalizeContext ( context ) ;
9270
93- // @ts -expect-error - isThenable returns the wrong type
9471 return withScope ( scope => {
9572 const hub = getCurrentHub ( ) ;
9673 const parentSpan = scope . getSpan ( ) ;
9774
9875 const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
9976 scope . setSpan ( activeSpan ) ;
10077
101- function finishAndSetSpan ( ) : void {
102- activeSpan && activeSpan . end ( ) ;
103- }
104-
105- let maybePromiseResult : T ;
106- try {
107- maybePromiseResult = callback ( activeSpan ) ;
108- } catch ( e ) {
109- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
110- finishAndSetSpan ( ) ;
111- throw e ;
112- }
113-
114- if ( isThenable ( maybePromiseResult ) ) {
115- return maybePromiseResult . then (
116- res => {
117- finishAndSetSpan ( ) ;
118- return res ;
119- } ,
120- e => {
121- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
122- finishAndSetSpan ( ) ;
123- throw e ;
124- } ,
125- ) ;
126- }
127-
128- finishAndSetSpan ( ) ;
129- return maybePromiseResult ;
78+ return handleCallbackErrors (
79+ ( ) => callback ( activeSpan ) ,
80+ ( ) => {
81+ // Only update the span status if it hasn't been changed yet
82+ if ( activeSpan && ( ! activeSpan . status || activeSpan . status === 'ok' ) ) {
83+ activeSpan . setStatus ( 'internal_error' ) ;
84+ }
85+ } ,
86+ ( ) => activeSpan && activeSpan . end ( ) ,
87+ ) ;
13088 } ) ;
13189}
13290
@@ -152,7 +110,6 @@ export function startSpanManual<T>(
152110) : T {
153111 const ctx = normalizeContext ( context ) ;
154112
155- // @ts -expect-error - isThenable returns the wrong type
156113 return withScope ( scope => {
157114 const hub = getCurrentHub ( ) ;
158115 const parentSpan = scope . getSpan ( ) ;
@@ -164,25 +121,15 @@ export function startSpanManual<T>(
164121 activeSpan && activeSpan . end ( ) ;
165122 }
166123
167- let maybePromiseResult : T ;
168- try {
169- maybePromiseResult = callback ( activeSpan , finishAndSetSpan ) ;
170- } catch ( e ) {
171- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
172- throw e ;
173- }
174-
175- if ( isThenable ( maybePromiseResult ) ) {
176- return maybePromiseResult . then (
177- res => res ,
178- e => {
179- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
180- throw e ;
181- } ,
182- ) ;
183- }
184-
185- return maybePromiseResult ;
124+ return handleCallbackErrors (
125+ ( ) => callback ( activeSpan , finishAndSetSpan ) ,
126+ ( ) => {
127+ // Only update the span status if it hasn't been changed yet, and the span is not yet finished
128+ if ( activeSpan && ! activeSpan . endTimestamp && ( ! activeSpan . status || activeSpan . status === 'ok' ) ) {
129+ activeSpan . setStatus ( 'internal_error' ) ;
130+ }
131+ } ,
132+ ) ;
186133 } ) ;
187134}
188135
0 commit comments