diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index 00088687bec6..9783d5a530b3 100644 --- a/packages/serverless/src/gcpfunction/cloud_events.ts +++ b/packages/serverless/src/gcpfunction/cloud_events.ts @@ -1,5 +1,5 @@ import { captureException, flush, getCurrentHub } from '@sentry/node'; -import { logger } from '@sentry/utils'; +import { isThenable, logger } from '@sentry/utils'; import { domainify, getActiveDomain, proxyFunction } from '../utils'; import type { CloudEventFunction, CloudEventFunctionWithCallback, WrapperOptions } from './general'; @@ -49,8 +49,6 @@ function _wrapCloudEventFunction( const activeDomain = getActiveDomain()!; // eslint-disable-line @typescript-eslint/no-non-null-assertion - activeDomain.on('error', captureException); - const newCallback = activeDomain.bind((...args: unknown[]) => { if (args[0] !== null && args[0] !== undefined) { captureException(args[0]); @@ -67,7 +65,22 @@ function _wrapCloudEventFunction( }); if (fn.length > 1) { - return (fn as CloudEventFunctionWithCallback)(context, newCallback); + let fnResult; + try { + fnResult = (fn as CloudEventFunctionWithCallback)(context, newCallback); + } catch (err) { + captureException(err); + throw err; + } + + if (isThenable(fnResult)) { + fnResult.then(null, err => { + captureException(err); + throw err; + }); + } + + return fnResult; } return Promise.resolve() diff --git a/packages/serverless/src/gcpfunction/events.ts b/packages/serverless/src/gcpfunction/events.ts index f1ad73379ca1..8f87bd478b0d 100644 --- a/packages/serverless/src/gcpfunction/events.ts +++ b/packages/serverless/src/gcpfunction/events.ts @@ -1,5 +1,5 @@ import { captureException, flush, getCurrentHub } from '@sentry/node'; -import { logger } from '@sentry/utils'; +import { isThenable, logger } from '@sentry/utils'; import { domainify, getActiveDomain, proxyFunction } from '../utils'; import type { EventFunction, EventFunctionWithCallback, WrapperOptions } from './general'; @@ -51,8 +51,6 @@ function _wrapEventFunction const activeDomain = getActiveDomain()!; // eslint-disable-line @typescript-eslint/no-non-null-assertion - activeDomain.on('error', captureException); - const newCallback = activeDomain.bind((...args: unknown[]) => { if (args[0] !== null && args[0] !== undefined) { captureException(args[0]); @@ -71,7 +69,22 @@ function _wrapEventFunction }); if (fn.length > 2) { - return (fn as EventFunctionWithCallback)(data, context, newCallback); + let fnResult; + try { + fnResult = (fn as EventFunctionWithCallback)(data, context, newCallback); + } catch (err) { + captureException(err); + throw err; + } + + if (isThenable(fnResult)) { + fnResult.then(null, err => { + captureException(err); + throw err; + }); + } + + return fnResult; } return Promise.resolve()