Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion packages/serverless/src/awslambda.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable max-lines */
import * as Sentry from '@sentry/node';
import {
captureException,
captureMessage,
Expand All @@ -8,7 +10,6 @@ import {
startTransaction,
withScope,
} from '@sentry/node';
import * as Sentry from '@sentry/node';
import { extractTraceparentData } from '@sentry/tracing';
import { Integration } from '@sentry/types';
import { isString, logger } from '@sentry/utils';
Expand Down Expand Up @@ -46,6 +47,12 @@ export interface WrapperOptions {
callbackWaitsForEmptyEventLoop: boolean;
captureTimeoutWarning: boolean;
timeoutWarningLimit: number;
/**
* Capture all errors when `Promise.allSettled` is returned by the handler
* The {@link wrapHandler} will not fail the lambda even if there are errors
* @default false
*/
captureAllSettledReasons: boolean;
}

export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices({ optional: true })];
Expand Down Expand Up @@ -86,9 +93,29 @@ function tryRequire<T>(taskRoot: string, subdir: string, mod: string): T {
return require(require.resolve(mod, { paths: [taskRoot, subdir] }));
}

/** */
function isPromiseAllSettledResult<T>(result: T[]): boolean {
return result.every(
v =>
Object.prototype.hasOwnProperty.call(v, 'status') &&
(Object.prototype.hasOwnProperty.call(v, 'value') || Object.prototype.hasOwnProperty.call(v, 'reason')),
);
}

type PromiseSettledResult<T> = { status: 'rejected' | 'fulfilled'; reason?: T };

/** */
function getRejectedReasons<T>(results: PromiseSettledResult<T>[]): T[] {
return results.reduce((rejected: T[], result) => {
if (result.status === 'rejected' && result.reason) rejected.push(result.reason);
return rejected;
}, []);
}

/** */
export function tryPatchHandler(taskRoot: string, handlerPath: string): void {
type HandlerBag = HandlerModule | Handler | null | undefined;

interface HandlerModule {
[key: string]: HandlerBag;
}
Expand Down Expand Up @@ -197,6 +224,7 @@ export function wrapHandler<TEvent, TResult>(
callbackWaitsForEmptyEventLoop: false,
captureTimeoutWarning: true,
timeoutWarningLimit: 500,
captureAllSettledReasons: false,
...wrapOptions,
};
let timeoutWarningTimer: NodeJS.Timeout;
Expand Down Expand Up @@ -272,6 +300,14 @@ export function wrapHandler<TEvent, TResult>(
// We put the transaction on the scope so users can attach children to it
scope.setSpan(transaction);
rv = await asyncHandler(event, context);

// We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
if (options.captureAllSettledReasons && Array.isArray(rv) && isPromiseAllSettledResult(rv)) {
const reasons = getRejectedReasons(rv);
reasons.forEach(exception => {
captureException(exception);
});
}
} catch (e) {
captureException(e);
if (options.rethrowAfterCapture) {
Expand Down
23 changes: 23 additions & 0 deletions packages/serverless/test/awslambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,29 @@ describe('AWSLambda', () => {
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setTag).toBeCalledWith('timeout', '1m40s');
});

test('captureAllSettledReasons disabled (default)', async () => {
const handler = () => Promise.resolve([{ status: 'rejected', reason: new Error() }]);
const wrappedHandler = wrapHandler(handler, { flushTimeout: 1337 });
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
expect(Sentry.captureException).toBeCalledTimes(0);
});

test('captureAllSettledReasons enable', async () => {
const error = new Error();
const error2 = new Error();
const handler = () =>
Promise.resolve([
{ status: 'rejected', reason: error },
{ status: 'fulfilled', value: undefined },
{ status: 'rejected', reason: error2 },
]);
const wrappedHandler = wrapHandler(handler, { flushTimeout: 1337, captureAllSettledReasons: true });
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
expect(Sentry.captureException).toHaveBeenNthCalledWith(1, error);
expect(Sentry.captureException).toHaveBeenNthCalledWith(2, error2);
expect(Sentry.captureException).toBeCalledTimes(2);
});
});

describe('wrapHandler() on sync handler', () => {
Expand Down