Skip to content

Commit 22a7cf3

Browse files
authored
ref(serverless): Added ignoreSentryErrors option for AWS lambda (#4620)
Add option to ignore internal `SentryError` errors, such as those that come from 429 or 413 responses.
1 parent 86a526a commit 22a7cf3

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

packages/serverless/src/awslambda.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@sentry/node';
1212
import { extractTraceparentData } from '@sentry/tracing';
1313
import { Integration } from '@sentry/types';
14-
import { isString, logger } from '@sentry/utils';
14+
import { isString, logger, SentryError } from '@sentry/utils';
1515
// NOTE: I have no idea how to fix this right now, and don't want to waste more time, as it builds just fine — Kamil
1616
// eslint-disable-next-line import/no-unresolved
1717
import { Context, Handler } from 'aws-lambda';
@@ -53,6 +53,7 @@ export interface WrapperOptions {
5353
* @default false
5454
*/
5555
captureAllSettledReasons: boolean;
56+
ignoreSentryErrors: boolean;
5657
}
5758

5859
export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices({ optional: true })];
@@ -224,6 +225,7 @@ export function wrapHandler<TEvent, TResult>(
224225
captureTimeoutWarning: true,
225226
timeoutWarningLimit: 500,
226227
captureAllSettledReasons: false,
228+
ignoreSentryErrors: false,
227229
...wrapOptions,
228230
};
229231
let timeoutWarningTimer: NodeJS.Timeout;
@@ -314,7 +316,13 @@ export function wrapHandler<TEvent, TResult>(
314316
clearTimeout(timeoutWarningTimer);
315317
transaction.finish();
316318
hub.popScope();
317-
await flush(options.flushTimeout);
319+
await flush(options.flushTimeout).catch(e => {
320+
if (options.ignoreSentryErrors && e instanceof SentryError) {
321+
logger.error(e);
322+
return;
323+
}
324+
throw e;
325+
});
318326
}
319327
return rv;
320328
};

packages/serverless/test/awslambda.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SentryError } from '@sentry/utils';
12
// NOTE: I have no idea how to fix this right now, and don't want to waste more time, as it builds just fine — Kamil
23
// eslint-disable-next-line import/no-unresolved
34
import { Callback, Handler } from 'aws-lambda';
@@ -177,6 +178,44 @@ describe('AWSLambda', () => {
177178
expect(Sentry.captureException).toHaveBeenNthCalledWith(2, error2);
178179
expect(Sentry.captureException).toBeCalledTimes(2);
179180
});
181+
182+
test('ignoreSentryErrors - with successful handler', async () => {
183+
const sentryError = new SentryError('HTTP Error (429)');
184+
jest.spyOn(Sentry, 'flush').mockRejectedValueOnce(sentryError);
185+
186+
const handledError = new Error('handled error, and we want to monitor it');
187+
const expectedSuccessStatus = { status: 'success', reason: 'we handled error, so success' };
188+
const handler = () => {
189+
Sentry.captureException(handledError);
190+
return Promise.resolve(expectedSuccessStatus);
191+
};
192+
const wrappedHandlerWithoutIgnoringSentryErrors = wrapHandler(handler, { ignoreSentryErrors: false });
193+
const wrappedHandlerWithIgnoringSentryErrors = wrapHandler(handler, { ignoreSentryErrors: true });
194+
195+
await expect(wrappedHandlerWithoutIgnoringSentryErrors(fakeEvent, fakeContext, fakeCallback)).rejects.toThrow(
196+
sentryError,
197+
);
198+
await expect(wrappedHandlerWithIgnoringSentryErrors(fakeEvent, fakeContext, fakeCallback)).resolves.toBe(
199+
expectedSuccessStatus,
200+
);
201+
});
202+
203+
test('ignoreSentryErrors - with failed handler', async () => {
204+
const sentryError = new SentryError('HTTP Error (429)');
205+
jest.spyOn(Sentry, 'flush').mockRejectedValueOnce(sentryError);
206+
207+
const criticalUnhandledError = new Error('critical unhandled error ');
208+
const handler = () => Promise.reject(criticalUnhandledError);
209+
const wrappedHandlerWithoutIgnoringSentryErrors = wrapHandler(handler, { ignoreSentryErrors: false });
210+
const wrappedHandlerWithIgnoringSentryErrors = wrapHandler(handler, { ignoreSentryErrors: true });
211+
212+
await expect(wrappedHandlerWithoutIgnoringSentryErrors(fakeEvent, fakeContext, fakeCallback)).rejects.toThrow(
213+
sentryError,
214+
);
215+
await expect(wrappedHandlerWithIgnoringSentryErrors(fakeEvent, fakeContext, fakeCallback)).rejects.toThrow(
216+
criticalUnhandledError,
217+
);
218+
});
180219
});
181220

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

0 commit comments

Comments
 (0)