kgniK
tRPC2y ago
1 reply
kgni

default TRPCError, try/catch & dealing with DatabaseErrors

Hi there!

I'm quite new to tRPC and was hoping that someone could help me out with some general questions related to errors and errorhandling. Thanks in advance!


1. Default TRPCError
It seems like that the fallback / default error that is thrown from the trpc server is a TRPCError with code
INTERNAL_SERVER_ERROR
- is this correct?

2. try/catch
If the above is true, I guess in some cases I don't even need to use a
try...catch
block?
Let's say I'm not checking any specific database errors or anything - then I could leave out
try...catch
all together - or is it considered a best practice to always just use
try...catch
for consistency when dealing with async operations, and then throw an
INTERNAL_SERVER_ERROR
manually?


3. Handling database errors
To combat the above
try...catch
consistency dillema, I'm thinking about implementing a general
errorHandler
that handles various database errors thrown by my postgres db.
This handler can then throw specific
TRPCErrors
depending on the the postgres errors. Additionally, I could extend the
TRPCError
class to include a translation string for my frontend.

Does this approach sound reasonable?
Was this page helpful?