Skip to content

Fix/nestjs cron issues #16792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 3, 2025

Conversation

0xbad0c0d3
Copy link
Contributor

@0xbad0c0d3 0xbad0c0d3 commented Jul 2, 2025

Nestjs sync task has the same issue as async tasks - they don't care about exceptions because nestjs @Cron() decorator automatically wraps it to try-catch block and logs an error. That's why we should capture exception in our @SentryCron() decorator.
Fixes also #16749

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

cod1k added 3 commits July 2, 2025 18:49
Add a command to delete the Sentry-related cache during the E2E test setup process. This ensures a clean testing environment and prevents potential issues caused by stale cache. This is why running tests locally after any change might be wrong. Not sure this is the best solution, or no. But, at least results are constant now
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.
Added error capturing for both synchronous and asynchronous exceptions in monitored methods. Incorporated `isThenable` checks to handle promises properly, ensuring robust error tracking with Sentry in both cases.
Copy link
Member

@andreiborza andreiborza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thank you for contributing this!

cc @chargome

@@ -66,6 +66,7 @@ async function run(): Promise<void> {
}

await asyncExec('pnpm clean:test-applications', { env, cwd: __dirname });
await asyncExec('pnpm cache delete "@sentry/*"', { env, cwd: __dirname });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Is this necessary? Did you run into issues without it? I think we should not add this in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest - no, it is not necessary to be here in this PR. But yes, you are going to run into an issue without this. The issue is that if you are doing any changes to any package and want to run e2e tests locally with yarn workspace @sentry-internal/e2e-tests test:run app-name your very first test will be executed as expected. But any other changes are not going to be reflected by pnpm install because of the cache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also rather get rid of this and if necessary address this in a different PR.

@andreiborza andreiborza requested a review from chargome July 2, 2025 16:52
cod1k added 3 commits July 3, 2025 08:12
Added JSDoc callback refference
Removed redundant else in if-else block
Updated the test to safely handle potential undefined values in the stacktrace frames. This prevents runtime errors during test execution and ensures robust validation of error handling logic.
…16749

Introduce a new endpoint `/crash-in-with-monitor/:id` that demonstrates error handling using Sentry's `withMonitor` API. Added tests to ensure the app gracefully handles exceptions and validates consistent PID behavior during monitoring.
@0xbad0c0d3
Copy link
Contributor Author

Added tests to cover #16749 and did some refactoring in withMonitor() function

Copy link
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@@ -66,6 +66,7 @@ async function run(): Promise<void> {
}

await asyncExec('pnpm clean:test-applications', { env, cwd: __dirname });
await asyncExec('pnpm cache delete "@sentry/*"', { env, cwd: __dirname });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also rather get rid of this and if necessary address this in a different PR.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Unrelated Cache Command in PR

The pnpm cache delete "@sentry/*" command was committed to dev-packages/e2e-tests/run.ts. This command is unrelated to the PR's main purpose of fixing NestJS cron issues, though it was intended to address a separate local development caching problem. Despite PR discussion consensus among reviewers and the author that it was unnecessary for this PR and should be handled separately, it remains in the committed code.

dev-packages/e2e-tests/run.ts#L68-L69

await asyncExec('pnpm clean:test-applications', { env, cwd: __dirname });
await asyncExec('pnpm cache delete "@sentry/*"', { env, cwd: __dirname });

Fix in Cursor


Was this report helpful? Give feedback by reacting with 👍 or 👎

@andreiborza andreiborza merged commit a75f456 into getsentry:develop Jul 3, 2025
146 checks passed
@0xbad0c0d3 0xbad0c0d3 deleted the fix/nestjs-cron-issues branch July 3, 2025 09:45
@andreiborza
Copy link
Member

andreiborza commented Jul 3, 2025

@0xbad0c0d3 thanks again for these contributions, if you'd like a free Sentry T-shirt please email me at my Github username -> firstname.lastname at sentry.io :)

mydea pushed a commit that referenced this pull request Jul 3, 2025
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #16792

Co-authored-by: andreiborza <168741329+andreiborza@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants