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

Function wrapStreamingHandler

packages/aws-serverless/src/sdk.ts:263–315  ·  view source on GitHub ↗
(
  handler: StreamifyHandler<TEvent, TResult>,
  options: WrapperOptions,
  startTime: number,
)

Source from the content-addressed store, hash-verified

261}
262
263function wrapStreamingHandler<TEvent, TResult>(
264 handler: StreamifyHandler<TEvent, TResult>,
265 options: WrapperOptions,
266 startTime: number,
267): StreamifyHandler<TEvent, TResult> {
268 let timeoutWarningTimer: NodeJS.Timeout | undefined;
269
270 const wrappedHandler = async (
271 event: TEvent,
272 responseStream: Parameters<StreamifyHandler<TEvent, TResult>>[1],
273 context: Context,
274 ): Promise<TResult> => {
275 context.callbackWaitsForEmptyEventLoop = options.callbackWaitsForEmptyEventLoop;
276
277 timeoutWarningTimer = setupTimeoutWarning(context, options);
278
279 async function processStreamingResult(): Promise<TResult> {
280 const scope = getCurrentScope();
281
282 try {
283 enhanceScopeWithEnvironmentData(scope, context, startTime);
284
285 responseStream.on('error', error => {
286 captureException(error, scope => markEventUnhandled(scope, 'auto.function.aws_serverless.stream'));
287 });
288
289 return await handler(event, responseStream, context);
290 } catch (e) {
291 // Errors should already be captured in the AwsLambda instrumentation's error handler,
292 // we capture them here just to be safe. Double captures are deduplicated by the SDK.
293 captureException(e, scope => markEventUnhandled(scope, 'auto.function.aws_serverless.handler'));
294 throw e;
295 } finally {
296 if (timeoutWarningTimer) {
297 clearTimeout(timeoutWarningTimer);
298 }
299 await flush(options.flushTimeout).catch(e => {
300 DEBUG_BUILD && debug.error(e);
301 });
302 }
303 }
304
305 return withScope(() => processStreamingResult());
306 };
307
308 const handlerWithSymbols = handler as unknown as Record<symbol, unknown>;
309 (wrappedHandler as unknown as Record<symbol, unknown>)[AWS_HANDLER_STREAMING_SYMBOL] =
310 handlerWithSymbols[AWS_HANDLER_STREAMING_SYMBOL];
311 (wrappedHandler as unknown as Record<symbol, unknown>)[AWS_HANDLER_HIGHWATERMARK_SYMBOL] =
312 handlerWithSymbols[AWS_HANDLER_HIGHWATERMARK_SYMBOL];
313
314 return wrappedHandler;
315}

Callers 1

wrapHandlerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected