Skip to content

Commit 74ea94a

Browse files
author
cod1k
committed
Add sync cron error handling and associated tests
Introduced a new `testSyncCronError` method to handle sync cron job errors and updated the async method for clarity. Updated tests to cover both async and sync cron scenarios, ensuring proper error handling and reporting to Sentry.
1 parent a23e18b commit 74ea94a

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.service.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,21 @@ export class AppService {
8585
only supports minute granularity, but we don't want to wait (worst case) a
8686
full minute for the tests to finish.
8787
*/
88-
@Cron('*/5 * * * * *', { name: 'test-cron-error' })
89-
@SentryCron('test-cron-error-slug', monitorConfig)
88+
@Cron('*/5 * * * * *', { name: 'test-async-cron-error' })
89+
@SentryCron('test-async-cron-error-slug', monitorConfig)
9090
async testCronError() {
91-
throw new Error('Test error from cron job');
91+
throw new Error('Test error from cron async job');
92+
}
93+
94+
/*
95+
Actual cron schedule differs from schedule defined in config because Sentry
96+
only supports minute granularity, but we don't want to wait (worst case) a
97+
full minute for the tests to finish.
98+
*/
99+
@Cron('*/5 * * * * *', { name: 'test-sync-cron-error' })
100+
@SentryCron('test-sync-cron-error-slug', monitorConfig)
101+
testSyncCronError() {
102+
throw new Error('Test error from cron sync job');
92103
}
93104

94105
async killTestCron(job: string) {

dev-packages/e2e-tests/test-applications/nestjs-basic/tests/cron-decorator.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,36 @@ test('Cron job triggers send of in_progress envelope', async ({ baseURL }) => {
6262
await fetch(`${baseURL}/kill-test-cron/test-cron-job`);
6363
});
6464

65-
test('Sends exceptions to Sentry on error in cron job', async ({ baseURL }) => {
65+
test('Sends exceptions to Sentry on error in async cron job', async ({ baseURL }) => {
6666
const errorEventPromise = waitForError('nestjs-basic', event => {
67-
return !event.type && event.exception?.values?.[0]?.value === 'Test error from cron job';
67+
return !event.type && event.exception?.values?.[0]?.value === 'Test error from cron async job';
6868
});
6969

7070
const errorEvent = await errorEventPromise;
7171

7272
expect(errorEvent.exception?.values).toHaveLength(1);
73-
expect(errorEvent.exception?.values?.[0]?.value).toBe('Test error from cron job');
7473
expect(errorEvent.contexts?.trace).toEqual({
7574
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
7675
span_id: expect.stringMatching(/[a-f0-9]{16}/),
7776
});
7877

7978
// kill cron so tests don't get stuck
80-
await fetch(`${baseURL}/kill-test-cron/test-cron-error`);
79+
await fetch(`${baseURL}/kill-test-cron/test-async-cron-error`);
80+
});
81+
82+
test('Sends exceptions to Sentry on error in sync cron job', async ({ baseURL }) => {
83+
const errorEventPromise = waitForError('nestjs-basic', event => {
84+
return !event.type && event.exception?.values?.[0]?.value === 'Test error from cron sync job';
85+
});
86+
87+
const errorEvent = await errorEventPromise;
88+
89+
expect(errorEvent.exception?.values).toHaveLength(1);
90+
expect(errorEvent.contexts?.trace).toEqual({
91+
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
92+
span_id: expect.stringMatching(/[a-f0-9]{16}/),
93+
});
94+
95+
// kill cron so tests don't get stuck
96+
await fetch(`${baseURL}/kill-test-cron/test-sync-cron-error`);
8197
});

0 commit comments

Comments
 (0)