functiondjF
tRPC12mo ago
4 replies
functiondj

Throw NOT_FOUND on nullish

I have many procedures like this:
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:
getFoo: protectedProcedure.query(async ({ input }) => {
  const maybeFoo = await db.foo.findFirst({ id: input.id })
  if (maybeFoo === undefined) {
    throw new TRPCError({ code: "NOT_FOUND" })
  }
  return maybeFoo
})
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:
export const throwOnNullish = <T>(promise: Promise<T>) =>
  promise.then((anything) => {
    if (anything === undefined || anything === null) {
      throw new TRPCError({ code: "NOT_FOUND" });
    }

    return anything;
  });
Was this page helpful?