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

All TRPCErrors thrown are wrapped by INTERNAL_SERVER_ERRORs when building with `esbuild`

Open harveylee opened this issue 1 year ago • 3 comments

Hi, I am using esbuild v0.17.4 to build and bundle the TRPC server-side code. By default, esbuild minification mangles the names of functions and properties, leading this condition to fail:

https://github.com/jlalmes/trpc-openapi/blob/aea45441af785518df35c2bc173ae2ea6271e489/src/adapters/node-http/errors.ts#L20

... which results in all TRPCErrors being wrapped INTERNAL_SERVER_ERRORs.

Is there some nuance that makes this alternative incorrect?

 if (cause instanceof TRPCError) {
    return cause as TRPCError;
  }

A workaround is to set keepNames to true in the esbuild build options (see https://esbuild.github.io/api/#keep-names).

harveylee avatar Mar 31 '23 04:03 harveylee

This line was copied over from tRPC main repo, do you have the same issue there?

https://github.com/trpc/trpc/blob/6b9193ea450349f85c5305514b89674def72e442/packages/server/src/internals/errors.ts#L17-L33

jlalmes avatar May 24 '23 03:05 jlalmes

I suspect I would, but I am only using TRPC with OpenAPI at the moment. I will put a little test case together without trpc-openapi when I get a chance.

harveylee avatar May 24 '23 04:05 harveylee

Sorry I never got around to a repro, but I did notice that a relevant patch of code was changed upstream and merged in trpc v10.38.5 (PR) - specifically by hardcoding TRPCError.name to 'TRPCError'.

harveylee avatar Oct 03 '23 22:10 harveylee