Skip to content

bug: FormData normalization errors in nextAppDirCaller leak as raw TypeError and skip onError #8124

Closed

Description

@Manan1801

Provide environment information

System:
  OS: macOS 27.0.1
Binaries:
  Node: 24.10.0
npmPackages:
  @trpc/server: 11.29.2
  typescript: 7.1.0
  react: 19.3.1

Describe the bug

When a Next.js server action is created with experimental_caller(nextAppDirCaller(...)) and
normalizeFormData is on (the default), the submitted FormData is converted with
formDataToObject before the procedure runs. That conversion throws on ordinary field names —
for example a form with both user and user.name, because one key is treated as a string and
the other as an object.

In nextAppDirCaller, the conversion runs before the invoke(...).catch(handleError) chain, so
when it throws:

  • a raw internal TypeError leaks to the caller instead of a TRPCError,
  • the onError handler is never called,
  • rethrowNextErrors is never called.

The other caller of the same helper (packages/next/src/app-dir/server.ts) already wraps it in
try/catch and raises a TRPCError, so the two code paths are inconsistent.

Expected: a TRPCError (INTERNAL_SERVER_ERROR), with onError called, like the other path.

Link to reproduction

https://stackblitz.com/github/trpc/examples-next-minimal-starter

To reproduce

import { initTRPC } from '@trpc/server';
import { nextAppDirCaller } from '@trpc/server/adapters/next-app-dir';

const t = initTRPC.create();

const action = t.procedure
  .experimental_caller(
    nextAppDirCaller({ onError: (o) => console.log('onError', o.error.code) }),
  )
  .mutation(() => 'ok');

const fd = new FormData();
fd.set('user', 'alice');
fd.set('user.name', 'bob');

await action(fd);
// Throws: TypeError: Cannot create property 'name' on string 'alice'
// onError is never logged. Expected a TRPCError and onError to fire.

Additional information

Root cause: in packages/server/src/adapters/next-app-dir/nextAppDirCaller.ts the
formDataToObject(input) call is outside the try/catch that handleError provides. I have a fix
and a regression test ready.

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. lx3133584 commented on Nov 18, 2026

    @lx3133584

    Claim.

    Confirmed on current main: in packages/server/src/adapters/next-app-dir/nextAppDirCaller.ts, the mutation branch calls formDataToObject(input) before the opts.invoke(...).catch(handleError(input)) chain, so a throw there escapes as a raw TypeError and skips both onError and rethrowNextErrors. The other caller of the same helper, packages/next/src/app-dir/server.ts:163-170, already wraps it and raises a TRPCError.

    The conversion itself fails when a field name is both a prefix and a leaf: set() assigns a string at user, then the next entry walks into it as if it were an object.

    Plan:

    1. Move the conversion inside the invoke chain for the mutation branch so the existing handleError handles it, which also keeps onError and rethrowNextErrors working.
    2. Leave query and the other caller alone.
    3. Add regression tests for a user + user.name form: the caller rejects with TRPCError and onError runs with the converted input.
  2. lx3133584 commented on Nov 18, 2026

    @lx3133584

    @Manan1801 has #8126 open for this, so I am stepping back rather than competing with it. Their patch takes the same approach I had and is the better one: it reuses the "Failed to convert FormData to an object" message, which keeps the two formDataToObject callers saying the same thing.

  3. added a commit that references this issue on Nov 23, 2026
    64679c0
  4. github-actions commented on Nov 24, 2026

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  5. locked as resolved and limited conversation to collaborators on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions