Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serverless): [v7] Check if cloud event callback is a function #11734

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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