Skip to content

feat: Distinguish handled and unhandled exceptions #6947

Done

Description

@marcospgp

Describe the feature you'd like to request

Currently, there is no simple way to distinguish between an intentionally thrown error that you want to send to the client side from an unhandled exception that you don't want to send to the client side and would instead want to pre-process and turn into a generic error message before sending it to the client.

Describe the solution you'd like to see

defaulting unhandled errors to a different UnhandledTrpcError class.

Describe alternate solutions

Creating a different error class manually.

Additional information

No response

👨‍👧‍👦 Contributing

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

Activity

  1. KATT commented on Sep 23, 2025

    @KATT
    Member

    They're all INTERNAL_SERVER_ERRORs, that's not enough?

    Feel free to have a stab at it

    • export function getTRPCErrorFromUnknown(cause: unknown): TRPCError {
      if (cause instanceof TRPCError) {
      return cause;
      }
      if (cause instanceof Error && cause.name === 'TRPCError') {
      // https://github.com/trpc/trpc/pull/4848
      return cause as TRPCError;
      }
      const trpcError = new TRPCError({
      code: 'INTERNAL_SERVER_ERROR',
      cause,
      });
      // Inherit stack from error
      if (cause instanceof Error && cause.stack) {
      trpcError.stack = cause.stack;
      }
      return trpcError;
      }
    • Anywhere we throw an error with INTERNAL_SERVER_ERROR
    • Update docs
    • You can write some tests in packages/tests/server/errors.test.ts
  2. KATT commented on Sep 23, 2025

    @KATT
    Member

    you could probably mostly vibe code it

  3. marcospgp commented on Sep 24, 2025

    @marcospgp
    Author

    They're all INTERNAL_SERVER_ERRORs, that's not enough?

    @KATT yea so what I want to do is to distinguish between internal server errors that i intentionally and explicitly want to send to the frontend versus those that were thrown by an unexpected error in the code that i didn't handle

    so in my trpc error handler i can decide whether to send an error to the front end untouched or whether i should hide the details if it's not something i handled explicitly

  4. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 v12 separates the two: errors a procedure declares with .errors() reach the client with their code and typed data, while anything else, a crash included, becomes an INTERNAL_SERVER_ERROR whose message is masked outside development (or exposed with exposeUnexpectedErrors: true). https://v12.trpc.io/v12/docs/server/errors#on-the-client. v12 isn't released yet and there's no ETA.

  5. added
    next-major/definite
    on Nov 2, 2026
  6. marcospgp commented on Nov 23, 2026

    @marcospgp
    Author

    That covers it, thanks! Errors declared with .errors() reach the client with their code and data, and everything else gets masked, which is the split I was after, so I'll close this.

  7. github-actions commented on Nov 25, 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.

  8. locked as resolved and limited conversation to collaborators on Nov 25, 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

    Labels

    next-major/definite

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions