fix(api-handler): narrow safeRequestProp catch + harden @source path resolution

Two carry-overs from the W7d dev-server repair, flagged for v1.4.26 in
the post-W7d follow-up list.

1. `safeRequestProp` previously caught every exception from the read
   callback so logging instrumentation never crashed the wrapped
   handler. That was overly broad — a regression in a header parser
   or a `null` access inside `read` would silently fall back to the
   default and hide the bug. The catch is now narrowed to the V8
   private-field `TypeError` that Next 16 raises when the request is
   a synthetic placeholder on force-static handlers; every other
   error rethrows. Three message-shape variants are recognised
   (`private member`, `private field`, `private name`) to cover
   current V8 + Bun + older runtimes.

2. `src/app/globals.css` now imports Tailwind with an explicit
   `source("../..")` base. Tailwind v4 auto-detection otherwise walks
   up from the CSS file's resolver root, which can land at an
   unexpected level depending on how the build tool reaches the
   stylesheet. Pinning the base mirrors the cwd-anchored fix the
   safety-contracts loader landed in commit `dd44a0d` and stops the
   planning / docs directories from leaking class-shaped fragments
   into the generated CSS regardless of where the build is invoked
   from.

The narrow-catch helper is exposed through a `__testables` export so
its contract can be unit-tested directly. The new test file covers:
  - the three private-field message variants classify as match
  - generic TypeErrors, RangeErrors, and bare Errors classify as
    non-match (must rethrow)
  - safeRequestProp returns the read result on a real request
  - safeRequestProp returns the fallback on a private-field TypeError
  - safeRequestProp rethrows any other exception

Verification:
  pnpm typecheck                           → clean
  pnpm lint                                → clean
  pnpm vitest run api-handler-safe-request-prop → 9 / 9 pass
This commit is contained in:
Marc-André Bombeck committed 2026-05-14 21:45:56 +02:00
1 parent 7b766bedab
commit 61d156e71b
3 files changed
+181 -3

No files matched your search

+25 -2
View File
@@ -40,6 +40,22 @@ export class HttpError extends Error {
// to `force-static` route handlers during dev). We probe defensively
// and fall back to safe defaults so logging instrumentation never
// crashes the handler.
//
// The catch is narrowed to the V8 private-field TypeError. Any other
// exception is a real bug in the read callback or in a downstream
// header parser and must surface — swallowing it here would hide
// regressions in production instrumentation.
function isPrivateFieldAccessError(err: unknown): boolean {
if (!(err instanceof TypeError)) return false;
const msg = err.message ?? "";
return (
msg.includes("private member") ||
msg.includes("private field") ||
// Bun / older V8 wording variant
msg.includes("private name")
);
}
function safeRequestProp<R>(
request: unknown,
read: (req: NextRequest) => R,
@@ -47,11 +63,18 @@ function safeRequestProp<R>(
): R {
try {
return read(request as NextRequest);
} catch {
return fallback;
} catch (err) {
if (isPrivateFieldAccessError(err)) return fallback;
throw err;
}
}
/** @internal — exposed for unit tests of the narrow-catch contract. */
export const __testables = {
safeRequestProp,
isPrivateFieldAccessError,
};
// Next.js route handlers come in two shapes — `(request)` for static routes
// and `(request, { params })` for dynamic ones. The variadic generic is the
// only signature TS accepts that covers both at the call site. The `any[]`