Riki
Riki6mo ago

Errors Ergonomics

Hello, two questions regarding Errors, Auth and Pagination Context: I use firebase authentication that I inject within my convex queries with the auth hook. So, at the end of the day, I get server-side the auth id of my users thanks to ctx.auth.getUserIdentity(). I used the approach to throw a ConvexError on the server if some query requests are not authenticated according to this doc. Problem: 1) It seems like the queries are still running when the JWT expires. So the queries throw an error because getUserIdentity returns undefined. I am not very used to auth, but wouldn't it be better that the convex auth hook in the client fetches a new JWT when the JWT is expired and goes to loading mode meanwhile? 2) When I hit an auth error, although that's not optimal, I decided to return "empty" values. Ever undefined for normal queries or an empty pagination response. I did it like below for pagination. However that still makes the UI crashes and convex servers are returning the following when my JWT expires: Uncaught Error: InvalidCursor: Tried to run a query starting from a cursor, but it looks like this cursor is from a different query. at async handler (../convex/usersVisited.ts:107:17). Is there a convenient value that is exported from convex to model a generic empty pagination result?
let authId: string;
try {
authId = await ensureAuthenticated(ctx);
} catch (error) {
return await ctx.db
.query('usersVisited')
.withIndex('by_toUserId_count', q =>
q.eq('toUserId', authId).gte('createdAt', 0).lt('createdAt', 0),
)
.order('desc')
.paginate(args.paginationOpts);
}

const lowerBounds = ....
const upperBounds = ....
const visitors = await ctx.db
.query('usersVisited')
.withIndex('by_toUserId_count', q =>
q
.eq('toUserId', authId)
.gte('createdAt', lowerBound)
.lt('createdAt', upperBound),
)
.order('desc')
.paginate(args.paginationOpts);
let authId: string;
try {
authId = await ensureAuthenticated(ctx);
} catch (error) {
return await ctx.db
.query('usersVisited')
.withIndex('by_toUserId_count', q =>
q.eq('toUserId', authId).gte('createdAt', 0).lt('createdAt', 0),
)
.order('desc')
.paginate(args.paginationOpts);
}

const lowerBounds = ....
const upperBounds = ....
const visitors = await ctx.db
.query('usersVisited')
.withIndex('by_toUserId_count', q =>
q
.eq('toUserId', authId)
.gte('createdAt', lowerBound)
.lt('createdAt', upperBound),
)
.order('desc')
.paginate(args.paginationOpts);
Thanks in advance!
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?