tRPCttRPC
Powered by
pes04P
tRPC•10mo ago•
6 replies
pes04

Question about onError and modifying error messages in tRPC

I’m trying to follow the tRPC docs on error handling and want to log detailed errors on the server by throwing specific exceptions but send a generic message to the client (see https://trpc.io/docs/server/error-handling#handling-errors).

I thought this would work:
onError: (opts) => {
  logError(`Error in TRPC in path=${opts.path}`, opts.error);

  opts.error = new TRPCError({
    code: 'INTERNAL_SERVER_ERROR',
    message: 'An error happened. Please try again',
  });
}
onError: (opts) => {
  logError(`Error in TRPC in path=${opts.path}`, opts.error);

  opts.error = new TRPCError({
    code: 'INTERNAL_SERVER_ERROR',
    message: 'An error happened. Please try again',
  });
}


But it doesn’t seem to have any effect — the original error is still sent to the client. According to the docs, I assumed mutating opts.error would override the one sent to the client. Am I misunderstanding something, or is there another way to do this?

Thanks in advance.
Error Handling | tRPC
Whenever an error occurs in a procedure, tRPC responds to the client with an object that includes an "error" property. This property contains all the information that you need to handle the error in the client.
Error Handling | tRPC
Solution
You want the errorFormatter
Jump to solution
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

tRPC onError
JoeJJoe / ❓-help
3y ago
Question about trpc revalidation method
dotdotdotDdotdotdot / ❓-help
3y ago
Useless TRPC Error messages: Error: ❌ tRPC failed on <no-path>
Yousef ShanawanyYYousef Shanawany / ❓-help
2y ago
onSuccess and onError in one time
SzymonSSzymon / ❓-help
15mo ago