Ashutosh Bind
Ashutosh Bind2w ago

Convex error handling best practices

I am new to convex, and looked up the relevant error handling docs. There are some Expected errors, and there could be convex-specific errors (network errs, etc) I had some questions regarding error retries and how to handle them
export const createCourse = mutation({
args: {
title: v.string(),
description: v.string(),

},
handler: async (ctx, args) => {
const userId = await getUserId(ctx);

try {
const course = await ctx.db.insert("course", {
title: args.title,
description: args.description,
ownerId: userId,
isPublished: false,
});

return course;
} catch (error) {
throw new ConvexError({
status: 500,
message: "Error creating course",
})
}
}
})
export const createCourse = mutation({
args: {
title: v.string(),
description: v.string(),

},
handler: async (ctx, args) => {
const userId = await getUserId(ctx);

try {
const course = await ctx.db.insert("course", {
title: args.title,
description: args.description,
ownerId: userId,
isPublished: false,
});

return course;
} catch (error) {
throw new ConvexError({
status: 500,
message: "Error creating course",
})
}
}
})
Note: the db query is used inside of a try catch if I handle a public mutation as this, wont the db errors now classify as my Application(expected) errors as well, if so, what's the best way to handle it, just let the db calls be outside of try-catches?
1 Reply
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!

Did you find this page helpful?