Throw NOT_FOUND on nullish
I have many procedures like this:
This example uses Drizzle, which doesn't have a
Since i want to
My research so far:
- Output validators are not a solution because when they fail it results in a
- Middleware sounds like it should be the natural solution but the documentation doesn't show any examples accessing procedure data, i.e. post-procedure middleware. There is a discussion on accessing req/res but the solution instructing to use context sounds very hacky and will definitely not get me type-safe results.
- There's also a Drizzle issue to add
- This actually works and is type-safe (removes nullish from resulting type) but kind of ugly in usage:
getFoo: protectedProcedure.query(({ input }) => db.foo.findFirst({ id: input.id })This example uses Drizzle, which doesn't have a
findFirstOrThrow convenience function.Since i want to
throw TRPCError when the DB record couldn't be found, i'd have to write this code everywhere:Is there a nicer way to achieve this when dealing with nullish return values?My research so far:
- Output validators are not a solution because when they fail it results in a
INTERNAL_SERVER_ERROR.- Middleware sounds like it should be the natural solution but the documentation doesn't show any examples accessing procedure data, i.e. post-procedure middleware. There is a discussion on accessing req/res but the solution instructing to use context sounds very hacky and will definitely not get me type-safe results.
- There's also a Drizzle issue to add
findFirstOrThrow from 2023 but it doesn't look like they'll implement this anytime soon.- This actually works and is type-safe (removes nullish from resulting type) but kind of ugly in usage: