How to throw a tRPC error if prisma query returns null
I tried the code in the photo but trpc query still returns null instead of raising an error.
12 Replies
You aren't awaiting
You have a promise
Your promise doesn't equal null
I realised and used .then but still can't catch the error using onError
That's not how that works, the thing that's throwing the tRPC error is the callback to .then not the query resolver
Use async/await
Thank you so much
Just leaving the code I ended up with just in case someone needs it in the future
Whyre you awaiting and then returning the promise
Doesn't that add unnecessary overhead
I'm pretty sure prisma has some sort of findUniqueOrThrow
It does but tRPC will treat that as internal server error
Ah good to know
You can go into the error shape overrides and make it treat it as a not found though
Oh! I totally missed that, thanks.