Why am I seeing 500 errors on responses to clients in production?
I'm running my trpc server with NODE_ENV=production with the expressjs adapter, and I'm getting this in the response of a client.
[{"error":{"message":"connect ECONNREFUSED 127.0.0.1:5432","code":-32603,"data":{"code":"INTERNAL_SERVER_ERROR","httpStatus":500,"path":"budget"}}}]
I would expected the message to not show up, and just give a generic error message.
Why don't these errors get written to stdout
by default instead? Or maybe I don't have it setup correctly?6 Replies
Solution
You can use an errorFormatter to customise this
But it looks like it’s working as intended, internal errors get sent to the frontend, and in this case your DB connection is getting passed on because you haven’t otherwise handled it
NODE_ENV should definitely vary some leakiness, I’d need to check how much it varies it
With regards to STDERR, you can use onError in your adapter to log this out
And I think only stacktrace info gets disabled by NODE_ENV https://trpc.io/docs/server/error-handling
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.
but shouldn't the message also be excluded? not just the stacktrace?
I was hoping that I did a misconfig, or in an unlikely case, that it was a bug.
I noticed I can config that out (although the only example is for next, I could potentially push a PL for that)
It’s a feature rather than a bug, for a production app you should generally have a error formatter
If we had strong default opinions it might stop you doing what you want, but this way you can be as strict as you like
ok, I see
thank you for your time