jsonnull / electron-trpc

Build type-safe Electron inter-process communication using tRPC
https://electron-trpc.dev/
MIT License
275 stars 27 forks source link

Unable to use instanceof TRPCClientError #209

Open Ryanjso opened 15 hours ago

Ryanjso commented 15 hours ago

I'm having trouble getting errors to show up correctly as instances of the TRPCClientError class. If you console.log(err.constructor.name) it's always _y

It seems it's being minified by Vite somewhere but I've played with the configuration a lot and can't seem to figure out anything different.

I realize this may be a Vite issue more so than an electron-trpc issue but I imagine everyone here is likely facing this so curious of a fix.

This issue happens in my own repo and this repos example basic-react project

import { TRPCClientError } from '@trpc/react-query'; // same issue even if you import from '@trpc/client'

 try {
          await mutateAsync();
        } catch (err) {
          if (err instanceof TRPCClientError) {
            // never happens :(
          } 
        }
Ryanjso commented 12 hours ago

This is my way of hacking it for now, but obviously less than ideal

export const isTRPCClientError = (error: unknown): error is TRPCClientError<AppRouter> => {
  return typeof error === 'object' && error !== null && error['name'] === 'TRPCClientError'
}

// inside catch block
if (isTRPCClientError(error)) {
        // error is now a TRPCClientError
}