|
| 1 | +import { SentryError } from '@sentry/utils'; |
1 | 2 | // 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
|
2 | 3 | // eslint-disable-next-line import/no-unresolved
|
3 | 4 | import { Callback, Handler } from 'aws-lambda';
|
@@ -177,6 +178,44 @@ describe('AWSLambda', () => {
|
177 | 178 | expect(Sentry.captureException).toHaveBeenNthCalledWith(2, error2);
|
178 | 179 | expect(Sentry.captureException).toBeCalledTimes(2);
|
179 | 180 | });
|
| 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 | + }); |
180 | 219 | });
|
181 | 220 |
|
182 | 221 | describe('wrapHandler() on sync handler', () => {
|
|
0 commit comments