Skip to content

Commit 7029da4

Browse files
Lms24lforst
andauthored
fix(serverless): Capture custom tags in error events of GCP functions (#7298)
Change the way we listen to errors by directly try/catching the function instead of listening to the domain's error callback. Co-authored-by: Luca Forstner <luca.forstner@sentry.io>
1 parent 4ae08fe commit 7029da4

File tree

1 file changed

+25
-11
lines changed
  • packages/serverless/src/gcpfunction

1 file changed

+25
-11
lines changed

packages/serverless/src/gcpfunction/http.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import type { AddRequestDataToEventOptions } from '@sentry/node';
22
import { captureException, flush, getCurrentHub } from '@sentry/node';
33
import { extractTraceparentData } from '@sentry/tracing';
4-
import { baggageHeaderToDynamicSamplingContext, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils';
5-
6-
import { domainify, getActiveDomain, proxyFunction } from './../utils';
4+
import {
5+
baggageHeaderToDynamicSamplingContext,
6+
isString,
7+
isThenable,
8+
logger,
9+
stripUrlQueryAndFragment,
10+
} from '@sentry/utils';
11+
12+
import { domainify, proxyFunction } from './../utils';
713
import type { HttpFunction, WrapperOptions } from './general';
814

915
// TODO (v8 / #5257): Remove this whole old/new business and just use the new stuff
@@ -105,13 +111,6 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
105111
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
106112
(res as any).__sentry_transaction = transaction;
107113

108-
// functions-framework creates a domain for each incoming request so we take advantage of this fact and add an error handler.
109-
// BTW this is the only way to catch any exception occured during request lifecycle.
110-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
111-
getActiveDomain()!.on('error', err => {
112-
captureException(err);
113-
});
114-
115114
// eslint-disable-next-line @typescript-eslint/unbound-method
116115
const _end = res.end;
117116
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -128,6 +127,21 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
128127
});
129128
};
130129

131-
return fn(req, res);
130+
let fnResult;
131+
try {
132+
fnResult = fn(req, res);
133+
} catch (err) {
134+
captureException(err);
135+
throw err;
136+
}
137+
138+
if (isThenable(fnResult)) {
139+
fnResult.then(null, err => {
140+
captureException(err);
141+
throw err;
142+
});
143+
}
144+
145+
return fnResult;
132146
};
133147
}

0 commit comments

Comments
 (0)