MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / wrapPromiseWithMethods

Function wrapPromiseWithMethods

packages/core/src/tracing/ai/utils.ts:308–342  ·  view source on GitHub ↗
(
  originalPromiseLike: Promise<R>,
  instrumentedPromise: Promise<R>,
  mechanismType: string,
)

Source from the content-addressed store, hash-verified

306 * forwarded to the original promise to maintain the SDK's API surface.
307 */
308export function wrapPromiseWithMethods<R>(
309 originalPromiseLike: Promise<R>,
310 instrumentedPromise: Promise<R>,
311 mechanismType: string,
312): Promise<R> {
313 // If the original result is not thenable, return the instrumented promise
314 if (!isThenable(originalPromiseLike)) {
315 return instrumentedPromise;
316 }
317
318 // Create a proxy that forwards Promise methods to instrumentedPromise
319 // and preserves additional methods from the original result
320 return new Proxy(originalPromiseLike, {
321 get(target: object, prop: string | symbol): unknown {
322 // For standard Promise methods (.then, .catch, .finally, Symbol.toStringTag),
323 // use instrumentedPromise to preserve Sentry instrumentation.
324 // For custom methods (like .withResponse()), use the original target.
325 const useInstrumentedPromise = prop in Promise.prototype || prop === Symbol.toStringTag;
326 const source = useInstrumentedPromise ? instrumentedPromise : target;
327
328 const value = Reflect.get(source, prop) as unknown;
329
330 // Special handling for .withResponse() to preserve instrumentation
331 // .withResponse() returns { data: T, response: Response, request_id: string }
332 if (prop === 'withResponse' && typeof value === 'function') {
333 return function wrappedWithResponse(this: unknown): unknown {
334 const originalWithResponse = (value as (...args: unknown[]) => unknown).call(target);
335 return createWithResponseWrapper(originalWithResponse, instrumentedPromise, mechanismType);
336 };
337 }
338
339 return typeof value === 'function' ? value.bind(source) : value;
340 },
341 }) as Promise<R>;
342}

Callers 4

utils.test.tsFile · 0.90
instrumentMethodFunction · 0.90
handleStreamingRequestFunction · 0.90
applyFunction · 0.90

Calls 1

isThenableFunction · 0.90

Tested by

no test coverage detected