Can you force the return of a handler when defining a custom query?
When using
convex-helpers
to define a customQuery
, is it possible to force the return of the handler? All the examples throw when there is, for example, an authentication issue.
Instead I'd like to have a type Result<T> = {success: true, data: T} | {success: false, code: number, message: string}
that all queries return, and then when there is an authentication issue force the handler to return {success: false, code: 401, message: "Invalid authentication."}
.
I'm looking at this: https://stack.convex.dev/custom-functions, which recommends using throws like this:
5 Replies
No, customQuery doesn't have this yet.
Instead you can wrap
query
manually, like here:
https://github.com/get-convex/convex-lucia-auth/blob/main/src/index.ts#L28-L53GitHub
convex-lucia-auth/src/index.ts at main · get-convex/convex-lucia-auth
Convex database adapter for Lucia Auth. Contribute to get-convex/convex-lucia-auth development by creating an account on GitHub.
Perfect thanks for the example, that's roughly what I got my way to, but was having trouble getting the types to pass
Yeah that's why @ian built customQuery to help with the types, but to get the return types correctly inferred is a bit trickier
One thing to consider @Kyle when making more robust error handling is to use ConvexError with structured fields for status codes etc.
it’s harder for react queries to gracefully handle them but in general they’re pretty handy for errors crossing api boundaries
I did try that at first; I believe I was running into problems with my setup with triggering boundaries while things were in a loading state. Like the first pass UserIdentity would be undefined or something and trigger the thrown convex error. That was a lot more annoying to deal with