Skip to content

Commit 6a8a410

Browse files
authored
fix(serverless): Check if cloud event callback is a function (#9044) (#11701)
1 parent ea717f4 commit 6a8a410

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

packages/google-cloud-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/google-cloud-serverless/test/gcpfunction/cloud_event.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,59 @@ describe('wrapCloudEventFunction', () => {
8484
expect(mockFlush).toBeCalledWith(2000);
8585
});
8686

87+
describe('wrapEventFunction() as Promise', () => {
88+
test('successful execution', async () => {
89+
const func: CloudEventFunction = _context =>
90+
new Promise(resolve => {
91+
setTimeout(() => {
92+
resolve(42);
93+
}, 10);
94+
});
95+
const wrappedHandler = wrapCloudEventFunction(func);
96+
await expect(handleCloudEvent(wrappedHandler)).resolves.toBe(42);
97+
98+
const fakeTransactionContext = {
99+
name: 'event.type',
100+
op: 'function.gcp.cloud_event',
101+
attributes: {
102+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
103+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event',
104+
},
105+
};
106+
107+
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
108+
expect(mockSpan.end).toBeCalled();
109+
expect(mockFlush).toBeCalledWith(2000);
110+
});
111+
112+
test('capture error', async () => {
113+
const error = new Error('wat');
114+
const handler: CloudEventFunction = _context =>
115+
new Promise((_, reject) => {
116+
setTimeout(() => {
117+
reject(error);
118+
}, 10);
119+
});
120+
121+
const wrappedHandler = wrapCloudEventFunction(handler);
122+
await expect(handleCloudEvent(wrappedHandler)).rejects.toThrowError(error);
123+
124+
const fakeTransactionContext = {
125+
name: 'event.type',
126+
op: 'function.gcp.cloud_event',
127+
attributes: {
128+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
129+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event',
130+
},
131+
};
132+
133+
expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
134+
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
135+
expect(mockSpan.end).toBeCalled();
136+
expect(mockFlush).toBeCalled();
137+
});
138+
});
139+
87140
test('capture error', async () => {
88141
const error = new Error('wat');
89142
const handler: CloudEventFunction = _context => {

0 commit comments

Comments
 (0)