Skip to content

Commit 8da1c8d

Browse files
authored
fix(serverless): [v7] Check if cloud event callback is a function (#11734)
Backport of #11701 to v7
1 parent da5ba09 commit 8da1c8d

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

packages/serverless/src/gcpfunction/cloud_events.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ function _wrapCloudEventFunction(
5656
DEBUG_BUILD && logger.error(e);
5757
})
5858
.then(() => {
59-
callback(...args);
59+
if (typeof callback === 'function') {
60+
callback(...args);
61+
}
6062
});
6163
});
6264

packages/serverless/test/gcpfunction.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,59 @@ describe('GCPFunction', () => {
472472
expect(mockFlush).toBeCalledWith(2000);
473473
});
474474

475+
describe('wrapEventFunction() as Promise', () => {
476+
test('successful execution', async () => {
477+
const func: CloudEventFunction = _context =>
478+
new Promise(resolve => {
479+
setTimeout(() => {
480+
resolve(42);
481+
}, 10);
482+
});
483+
const wrappedHandler = wrapCloudEventFunction(func);
484+
await expect(handleCloudEvent(wrappedHandler)).resolves.toBe(42);
485+
486+
const fakeTransactionContext = {
487+
name: 'event.type',
488+
op: 'function.gcp.cloud_event',
489+
attributes: {
490+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
491+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event',
492+
},
493+
};
494+
495+
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
496+
expect(mockSpan.end).toBeCalled();
497+
expect(mockFlush).toBeCalledWith(2000);
498+
});
499+
500+
test('capture error', async () => {
501+
const error = new Error('wat');
502+
const handler: CloudEventFunction = _context =>
503+
new Promise((_, reject) => {
504+
setTimeout(() => {
505+
reject(error);
506+
}, 10);
507+
});
508+
509+
const wrappedHandler = wrapCloudEventFunction(handler);
510+
await expect(handleCloudEvent(wrappedHandler)).rejects.toThrowError(error);
511+
512+
const fakeTransactionContext = {
513+
name: 'event.type',
514+
op: 'function.gcp.cloud_event',
515+
attributes: {
516+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
517+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event',
518+
},
519+
};
520+
521+
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
522+
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
523+
expect(mockSpan.end).toBeCalled();
524+
expect(mockFlush).toBeCalled();
525+
});
526+
});
527+
475528
test('capture error', async () => {
476529
const error = new Error('wat');
477530
const handler: CloudEventFunction = _context => {

0 commit comments

Comments
 (0)