Convex Errors types

if (!identity) {
throw new ConvexError({
message: MISSING_AUTH,
severity: 'WARN',
});
}
if (!identity) {
throw new ConvexError({
message: MISSING_AUTH,
severity: 'WARN',
});
}
im getting no type defs for ConvexError?
export class ConvexError<TData extends Value> extends Error {
name = "ConvexError";
data: TData;
[IDENTIFYING_FIELD] = true;

constructor(data: TData) {
super(typeof data === "string" ? data : stringifyValueForError(data));
this.data = data;
}
}
export class ConvexError<TData extends Value> extends Error {
name = "ConvexError";
data: TData;
[IDENTIFYING_FIELD] = true;

constructor(data: TData) {
super(typeof data === "string" ? data : stringifyValueForError(data));
this.data = data;
}
}
The types for the class are weird?
6 Replies
ballingt
ballingt2y ago
@CodingWithJamal what version of Convex do you have installed? Can you say more about "no type defs," what are you expecting? Is this about what you get when you command-click in VS Code? I'd also like to hear more about the types for the class being weird, maybe this is the generic?
CodingWithJamal
CodingWithJamalOP2y ago
1.3.1 and im saying in the news postyou show and example of:
throw new ConvexError({
message: "Empty text not allowed"
severity: "FATAL",
});
throw new ConvexError({
message: "Empty text not allowed"
severity: "FATAL",
});
So i was expecting when i click message in my ide to be taken to the types but nothing happens. And also for the severity what are the other options? Should it not be like: severity: "FATAL" | "WARN" etc.. in the source code? how do i know the other options than FATAL?
ballingt
ballingt2y ago
Ah ok, yeah unfortunately clicking on an error does not jump to the source where it was defined — there are some subclassing approaches we looked at that could make this work, but inheritence / instanceof checks are tricky over the wire. Re
severity: "FATAL" | "WARN" etc.. in the source code?
This is arbitrary data, it's just an example; if a field called severity is useful to you you can add it to errors, but there's nothing special about any field in a ConvexError except for message OK this makes sense, the reason these are missing is that ConvexError doesn't require any options. If you define your own types or subclasses of ConvexError, those can have specific types, but be very careful not to use instanceof when checking for these.
CodingWithJamal
CodingWithJamalOP2y ago
I see, so is ConvexErrors only main function just to give the client contex to errors from the server side? Other than that its a wrapper for native Error type
ballingt
ballingt2y ago
Something like
type ValidationError = ConvexError<{message: string, kind: 'validation', errorType: "serious" | "not serious"}>

// use this function in Convex functions
function throwValidation(message) {
throw new ConvexError({message, kind: "validation", errorType: "serious"})
};

type AllErrors = ValidationError | AnotherKindOfError | ...
type ValidationError = ConvexError<{message: string, kind: 'validation', errorType: "serious" | "not serious"}>

// use this function in Convex functions
function throwValidation(message) {
throw new ConvexError({message, kind: "validation", errorType: "serious"})
};

type AllErrors = ValidationError | AnotherKindOfError | ...
would let these by typed: when you handle them, you can check kind to know what data to expect That's right, the difference between a normal error and a ConvexError is that only ConvexErrors get this data passed back to the client. The names of normal errors or the data attached might leak secret information to clients, so this needs to be an explicit choice.
CodingWithJamal
CodingWithJamalOP2y ago
okay thanks for the information!

Did you find this page helpful?