trpc-chrome icon indicating copy to clipboard operation
trpc-chrome copied to clipboard

`getErrorFromUnknown` mismatch TRPCError when bundler minified the TRPCError class

Open NotEvenANeko opened this issue 1 year ago • 0 comments

Vite with build.minify on will minified the name of TRPCError so cause instanceof Error && cause.name === 'TRPCError' in getErrorFromUnknown will always get false.

So if I throw a TRPCError in procedure and handle it in onError option, the opts.error will be a TRPCError with code === 'INTERNAL_SERVER_ERROR' and the cause in my error.

This is the first issue, and next section might be second issue but...I just write it here.

After I patched getErrorFromUnknown with (cause instanceof Error && cause.name === 'TRPCError') || cause instance TRPCError (server_1.TRPCError in compiled outputs), I found out that the TRPCError from error.js and my code through import { TRPCError } from '@trpc/server' is not the same TRPCError...

It might be because the compiled outputs is in CommonJS which require the @trpc/server and I imported the @trpc/server with ESModule import, vite bundled both TRPCError of them, two different TRPCError-s.

Having an esm output might be helpful.

Dependencies:

  • @trpc/server: 10.33.0
  • trpc-chrome: 1.0.0
  • vite: 4.3.8

NotEvenANeko avatar Jul 26 '23 17:07 NotEvenANeko