Skip to content

Commit

Permalink
fix(serverless): [v7] Check if cloud event callback is a function (#1…
Browse files Browse the repository at this point in the history
…1734)

Backport of #11701 to
v7
  • Loading branch information
AbhiPrasad authored Apr 23, 2024
1 parent da5ba09 commit 8da1c8d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/serverless/src/gcpfunction/cloud_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ function _wrapCloudEventFunction(
DEBUG_BUILD && logger.error(e);
})
.then(() => {
callback(...args);
if (typeof callback === 'function') {
callback(...args);
}
});
});

Expand Down
53 changes: 53 additions & 0 deletions packages/serverless/test/gcpfunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,59 @@ describe('GCPFunction', () => {
expect(mockFlush).toBeCalledWith(2000);
});

describe('wrapEventFunction() as Promise', () => {
test('successful execution', async () => {
const func: CloudEventFunction = _context =>
new Promise(resolve => {
setTimeout(() => {
resolve(42);
}, 10);
});
const wrappedHandler = wrapCloudEventFunction(func);
await expect(handleCloudEvent(wrappedHandler)).resolves.toBe(42);

const fakeTransactionContext = {
name: 'event.type',
op: 'function.gcp.cloud_event',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event',
},
};

expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
expect(mockSpan.end).toBeCalled();
expect(mockFlush).toBeCalledWith(2000);
});

test('capture error', async () => {
const error = new Error('wat');
const handler: CloudEventFunction = _context =>
new Promise((_, reject) => {
setTimeout(() => {
reject(error);
}, 10);
});

const wrappedHandler = wrapCloudEventFunction(handler);
await expect(handleCloudEvent(wrappedHandler)).rejects.toThrowError(error);

const fakeTransactionContext = {
name: 'event.type',
op: 'function.gcp.cloud_event',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event',
},
};

expect(mockStartSpanManual).toBeCalledWith(fakeTransactionContext, expect.any(Function));
expect(mockCaptureException).toBeCalledWith(error, expect.any(Function));
expect(mockSpan.end).toBeCalled();
expect(mockFlush).toBeCalled();
});
});

test('capture error', async () => {
const error = new Error('wat');
const handler: CloudEventFunction = _context => {
Expand Down

0 comments on commit 8da1c8d

Please sign in to comment.