From f3e0cff4cd085eff7e49b30c721e48d96a92ec70 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 28 Feb 2023 16:42:48 +0100 Subject: [PATCH 1/2] fix(serverless): Capture custom tags in GCP background function error events --- .../src/gcpfunction/cloud_events.ts | 2 -- packages/serverless/src/gcpfunction/events.ts | 21 +++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index 00088687bec6..be85afc72fba 100644 --- a/packages/serverless/src/gcpfunction/cloud_events.ts +++ b/packages/serverless/src/gcpfunction/cloud_events.ts @@ -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]); 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() From 592b5ef85d31ed2a07232f8f1ea1c59aaa444ed6 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 28 Feb 2023 17:01:46 +0100 Subject: [PATCH 2/2] fix(serverless): Capture custom tags in GCP CloudEvent function error events --- .../src/gcpfunction/cloud_events.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index be85afc72fba..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'; @@ -65,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()