tRPCttRPC
Powered by
kgniK
tRPC•2y 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
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
try...catch
block?
Let's say I'm not checking any specific database errors or anything - then I could leave out
try...catch
try...catch
all together - or is it considered a best practice to always just use
try...catch
try...catch
for consistency when dealing with async operations, and then throw an
INTERNAL_SERVER_ERROR
INTERNAL_SERVER_ERROR
manually?


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

Does this approach sound reasonable?
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Catch TRPCError, ZoddError on the front-end
CaptainCCaptain / ❓-help
4y ago
TRPCError that has TRPCError as cause
TkDodo 🔮TTkDodo 🔮 / ❓-help
3y ago
Throw custom TRPCError with specific cause
NeonNNeon / ❓-help
4y ago
Can i use try/catch in procedures?
Answer OverflowAAnswer Overflow / ❓-help
3y ago