Repository navigation
Description
Is there an existing issue for this?
- I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- I have reviewed the documentation https://docs.sentry.io/
- I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/nextjs
SDK Version
9.17.0
Framework Version
React Next 15.3.1
Link to Sentry event
Reproduction Example/SDK Setup
Create a repo with minimal code that reproduces this https://github.com/17Amir17/SentryBug
Repo contains a simple nextjs app with trpc and sentry
One route called getCats this route accepts two params one which when true throws an exception and the the other contains an id which gets added as a tag - https://github.com/17Amir17/SentryBug/blob/main/src/pages/api/trpc/%5Btrpc%5D.ts
Steps to Reproduce
There are two problem that I see, to reproduce the first
- Click "throw error" button
- Two calls to the same trpc route will be called in batch with different parameters
- A tag is added with the cat id
- And error is thrown
- 1 sentry event is created
- Tag does not match the context
To reproduce the second
- Click fetch button
- Two calls to the same trpc route will be called in batch with different parameters
- A tag is added with the cat id
- 2 sentry events are created
- Tag does not match context
Expected Result
For both issues I'd expect to see two events and for the context to match the tag
Actual Result
For the first issue the tags do not match the context and there is only 1 event created
For the second issue the there are two events but the tag does not match the context
Metadata
Metadata
Assignees
Projects
- StatusShow more project fieldsNo status

Activity
andreiborza commentedon May 14, 2025
Hi @17Amir17, thanks for filing this.
This happens because there's no automatic scope forking going on for the middleware. We're looking into possible workarounds/solutions.
andreiborza commentedon May 14, 2025
Because there's no automatic scope forking, you'd have to fork your own scope using for example the
Sentry.withScopeapi.Your trpc api would have to look something like this:
... .query(async ({ input }) => { return Sentry.withScope(async (scope) => { scope.setTag("catRequestId", input.catRequestId); await new Promise((resolve) => setTimeout(resolve, 1000)); if (input.shouldThrow) { throw new Error("Error triggered by shouldThrow in getCats"); } Sentry.captureMessage("getCats success"); return ["Whiskers", "Mittens", "Shadow"]; }); }), });Now, this works well for the second case, but it does not work for the first case because scope data is picked up when the error is captured, which happens in our trpc middleware. By then, the scope we fork ourselves is already gone and so is its data.
If you want to solve the first case, you'd have to manually try/catch throwing code and capture the error manually using
Sentry.captureException:... .query(async ({ input }) => { return Sentry.withScope(async (scope) => { scope.setTag("catRequestId", input.catRequestId); await new Promise((resolve) => setTimeout(resolve, 1000)); if (input.shouldThrow) { try { throw new Error("Error triggered by shouldThrow in getCats"); } catch (error) { Sentry.captureException(error); } return; } Sentry.captureMessage("getCats success"); return ["Whiskers", "Mittens", "Shadow"]; }); }),Hope this helps.
feat(node): Adds isolation scope forking to tRPC middleware
github-actions commentedon May 19, 2025
A PR closing this issue has just been released 🚀
This issue was referenced by PR #16296, which was included in the 9.20.0 release.
andreiborza commentedon May 19, 2025
@17Amir17 this should now work out of the box without having to fork the scope yourself.
17Amir17 commentedon May 19, 2025
BEAST thanks <3