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
56 changes: 28 additions & 28 deletions packages/serverless/src/awslambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,35 +319,35 @@ export function wrapHandler<TEvent, TResult>(
});
}

const scope = hub.pushScope();
let rv: TResult;
try {
enhanceScopeWithEnvironmentData(scope, context, START_TIME);
if (options.startTrace) {
enhanceScopeWithTransactionData(scope, context);
// 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, scope => markEventUnhandled(scope));
return withScope(async scope => {
let rv: TResult;
try {
enhanceScopeWithEnvironmentData(scope, context, START_TIME);
if (options.startTrace) {
enhanceScopeWithTransactionData(scope, context);
// 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, scope => markEventUnhandled(scope));
});
}
} catch (e) {
captureException(e, scope => markEventUnhandled(scope));
throw e;
} finally {
clearTimeout(timeoutWarningTimer);
transaction?.finish();
await flush(options.flushTimeout).catch(e => {
DEBUG_BUILD && logger.error(e);
});
}
} catch (e) {
captureException(e, scope => markEventUnhandled(scope));
throw e;
} finally {
clearTimeout(timeoutWarningTimer);
transaction?.finish();
hub.popScope();
await flush(options.flushTimeout).catch(e => {
DEBUG_BUILD && logger.error(e);
});
}
return rv;
return rv;
});
};
}
9 changes: 4 additions & 5 deletions packages/serverless/test/awslambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ describe('AWSLambda', () => {
});

test('captureTimeoutWarning enabled (default)', async () => {
expect.assertions(2);

const handler: Handler = (_event, _context, callback) => {
setTimeout(() => {
callback(null, 42);
Expand All @@ -105,14 +103,13 @@ describe('AWSLambda', () => {
const wrappedHandler = wrapHandler(handler);
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);

expect(Sentry.withScope).toBeCalledTimes(2);
expect(Sentry.captureMessage).toBeCalled();
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeScope.setTag).toBeCalledWith('timeout', '1s');
});

test('captureTimeoutWarning disabled', async () => {
expect.assertions(2);

const handler: Handler = (_event, _context, callback) => {
setTimeout(() => {
callback(null, 42);
Expand All @@ -123,8 +120,10 @@ describe('AWSLambda', () => {
});
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);

expect(Sentry.withScope).not.toBeCalled();
expect(Sentry.withScope).toBeCalledTimes(1);
expect(Sentry.captureMessage).not.toBeCalled();
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeScope.setTag).not.toBeCalledWith('timeout', '1s');
});

test('captureTimeoutWarning with configured timeoutWarningLimit', async () => {
Expand Down