TkDodo 🔮
TkDodo 🔮2y ago

clear cookie onError

I'd like to clear a cookie (in nextjs) when a 403 error happens. I've been looking at the global error handling: https://trpc.io/docs/error-handling#handling-errors and this looks like the right place, except that I don't have access to the response , which would be needed to clear a cookie. Is there a better place to do this, or is there a reason there is no access to the res ?
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.
4 Replies
Alex / KATT 🐱
Middlewares are more flexible, have you tried that?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
TkDodo 🔮
TkDodo 🔮2y ago
I added a function to remove the cookies into the ctx - that works. Haven't looked at middlewares - the logging example shows how I can await the result and then act on it. I'll try if this also works for errors. Not sure if I'd have to try/catch the const result = await next()
Alex / KATT 🐱
The result never throws but returns an Either style thing Clearly we need to document it better haha