You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TRPC Middleware Context not Working as Expected with Batch Requests #16262
Because there's no automatic scope forking, you'd have to fork your own scope using for example the Sentry.withScope api.
Your trpc api would have to look something like this:
...
.query(async({ input })=>{returnSentry.withScope(async(scope)=>{scope.setTag("catRequestId",input.catRequestId);awaitnewPromise((resolve)=>setTimeout(resolve,1000));if(input.shouldThrow){thrownewError("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 })=>{returnSentry.withScope(async(scope)=>{scope.setTag("catRequestId",input.catRequestId);awaitnewPromise((resolve)=>setTimeout(resolve,1000));if(input.shouldThrow){try{thrownewError("Error triggered by shouldThrow in getCats");}catch(error){Sentry.captureException(error);}return;}Sentry.captureMessage("getCats success");return["Whiskers","Mittens","Shadow"];});}),
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