tRPCttRPC
Powered by
functiondjF
tRPC•13mo 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 })
getFoo: protectedProcedure.query(({ input }) => db.foo.findFirst({ id: input.id })

This example uses Drizzle, which doesn't have a
findFirstOrThrow
findFirstOrThrow
convenience function.
Since i want to
throw TRPCError
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
})
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
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
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;
  });
export const throwOnNullish = <T>(promise: Promise<T>) =>
  promise.then((anything) => {
    if (anything === undefined || anything === null) {
      throw new TRPCError({ code: "NOT_FOUND" });
    }

    return anything;
  });
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

throw custom error
player1Pplayer1 / ❓-help
2y ago
Can't throw error
DavidDDavid / ❓-help
2y ago
TRPCContextState not found in v10
NickNNick / ❓-help
3y ago
Throw Validation error inside the procedure?
SimonSSimon / ❓-help
10mo ago