KyleK
Convex Community3y ago
5 replies
Kyle

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:

const userQuery = customQuery(
  query,
  customCtx(async (ctx) => {
    const user = await getUser(ctx);
    if (!user) throw new Error("Authentication required");
    return { user, db };
  })
);
Was this page helpful?