Does ConvexError replace Error?
Normally, when I create REST APIs, I distinguish between errors that are the fault of the user (do not have permission) and those of the developer (500 internal server error, etc.) through the status codes. With Convex, as the documentation says, it is better to throw errors that are then caught directly on the front-end.
I am thinking about using ConvexError for developer errors and Error for user errors. Is ConvexError supposed to be used for all errors, or is this up to the developer? Additionally, since these will both be thrown on the frontend, what is the recommended way I distinguish between them in my ErrorBoundary?
5 Replies
ConvexError can be used to transmit details about the error to the client.
Other Errors are opaque when sent by Production deployments (backend), so they won’t be distinguishable.
Hence you want to use ConvexError for user errors, and Errors for any unexpected/developer errors.
You can use the same catching logic in an ErrorBoundary as described here:
https://docs.convex.dev/functions/error-handling/application-errors
Application Errors | Convex Developer Hub
If you have expected ways your functions might fail, you can either return
Oh perfect, I didn't realize the ConvexError takes an object literal, thank you.
can someone tell me why the exception shows in console ? and is there any workaround to fix this ?

this is my code just in case
import { v, ConvexError } from "convex/values";
import { mutation } from "../_generated/server";
export const validateUser = mutation({
args: {
email: v.string(),
password: v.string(),
},
handler: async (ctx, args) => {
const user = await ctx.db.query("users").filter((q) => q.eq(q.field('email'), args.email)).filter((q) => q.eq(q.field('password'), args.password)).first();
if (!user) {
throw new ConvexError({
code: 123,
severity: "high",
});
}
return user;
},
});