scrutinize the pattern being used: EXPECTED_TRPC_ERRORS and all the changes we are making.
is this truly the best long-term maintainbility and best code quality solution we can design.
we need to ensure that our log and sentry is best set up for actual proper debug and logging.
consider designing the most accretive, innovative, radical and creative solution that will 100x developer productivity.
Assistant· claude-opus-4-6
Let me deeply understand the current state before critiquing the plan.
Assistant· claude-opus-4-6
Now let me check one more thing — whether tRPC exposes HTTP status derivation.
Assistant· claude-opus-4-6
Now let me verify the getHTTPStatusCodeFromError export surface and check Sentry APIs.
Assistant· claude-opus-4-6
User
ys
Assistant· claude-opus-4-6
Let me read the files I need to modify, then implement.
Now I have the full picture. Here's my deep critique and a redesigned approach.
Critique of the Current Plan
The plan is solid consolidation — it correctly identifies the three overlapping layers and collapses them. But it has five structural problems that will cost you later:
1. EXPECTED_TRPC_ERRORS is a hand-maintained reimplementation of HTTP semantics
tRPC v11.12.0 already ships JSONRPC2_TO_HTTP_CODE which maps every error code to its HTTP status. The classification "expected vs unexpected" is literally "< 500 vs >= 500". Your 10-member set:
Misses 6 valid client-error codes that tRPC v11.12.0 defines: PAYMENT_REQUIRED (402), METHOD_NOT_SUPPORTED (405), TIMEOUT (408), PAYLOAD_TOO_LARGE (413), UNSUPPORTED_MEDIA_TYPE (415), PRECONDITION_REQUIRED (428). If someone throws new TRPCError({ code: "TIMEOUT" }) today, it hits Sentry as if it's a bug.
Must be manually updated whenever tRPC adds codes. They just added PAYMENT_REQUIRED and UNSUPPORTED_MEDIA_TYPE in v11 — your set doesn't have them.
Exists in a separate package (@repo/lib) from where it's semantically defined (tRPC itself), creating a coupling that adds no value.
tRPC exports getHTTPStatusCodeFromError from @trpc/server/http. One function call replaces the entire constant, forever:
2. captureException(result.error) is bare — Sentry events lack actionable context
The plan calls captureException(result.error) with zero additional context. When that event appears in Sentry, a developer sees a stack trace but can't immediately answer: which procedure? what type? how long did it run? who was the caller?
The scope context via scope.setContext("trpc", trpcContext) helps, but Sentry tags are what enable filtering/grouping in the Issues dashboard. The plan misses this.
3. No log-to-trace correlation
You have two observability backends (BetterStack for logs, Sentry for errors/traces). When an error fires in Sentry, there's no way to find the corresponding request journal in BetterStack, and vice versa. The requestId is in logs but not in Sentry. The Sentry traceId is in Sentry but not in logs. This is the #1 productivity killer in production debugging.
4. 90% code duplication between app and platform middleware
The plan copy-pastes the entire middleware into both api/app/src/trpc.ts and api/platform/src/trpc.ts. The only difference is auth context spreading (userId/orgId vs caller). This means every future change to observability logic must be applied twice.
5. beforeSend still uses its own constant
After all this consolidation, instrumentation.ts would import EXPECTED_TRPC_ERRORS from @repo/lib — still a manual list. It should use the same getHTTPStatusCodeFromError derivation.
// apps/app/src/instrumentation.ts — no more EXPECTED_TRPC_CODES constant
import { getHTTPStatusCodeFromError } from "@trpc/server/http";
beforeSend(event, hint) {
const err = hint.originalException;
if (err instanceof TRPCError && getHTTPStatusCodeFromError(err) < 500) {
return null;
}
return event;
}
4. Route handlers become trivial
Same as the plan — strip onError, captureException, EXPECTED_TRPC_ERRORS. No change needed here.
What this gets you
Dimension
Current Plan
Redesign
Constants to maintain
1 shared (10 members)
0 — derived from tRPC
Coverage of tRPC codes
10 of 16 client codes
All current + future
Middleware code copies
2 (app + platform)
1 factory + 2 three-line consumers
Log → Sentry correlation
None
traceId in every log entry
Sentry → procedure filtering
Not possible
trpc.path tag on every event
captureException context
Bare
Tags + extra metadata
Future tRPC version upgrade
Manual audit of constant
Zero-touch
The 100x insight
The traceId correlation is the real multiplier. When a Sentry alert fires at 3am, today you see a stack trace and have to grep BetterStack logs by timestamp and hope the procedure path matches. With traceId in every structured log, you paste the Sentry trace ID into BetterStack's search and get the full request journal — every log line that happened inside that procedure call — instantly.
The zero-constant derivation eliminates an entire category of bugs (the "we added a new error code but forgot to update the set" class) and means you never think about tRPC error classification again.
Want me to revise the plan with this approach and implement it?