Custom Errors
Last updated:
Creating Custom Errors
Basic Custom Error
import { VisulimaError } from "@visulima/error";
class ValidationError extends VisulimaError {
constructor(message: string, field?: string) {
super({
name: "ValidationError",
message,
});
if (field) {
this.hint = `Field '${field}' is invalid`;
}
}
}With Metadata
class APIError extends VisulimaError {
public statusCode: number;
public endpoint: string;
constructor(message: string, statusCode: number, endpoint: string) {
super({
name: "APIError",
message,
});
this.statusCode = statusCode;
this.endpoint = endpoint;
}
}
const error = new APIError("Request failed", 500, "/api/users");Error Chains
const dbError = new Error("Connection refused");
const appError = new VisulimaError({
name: "ApplicationError",
message: "Failed to fetch user",
cause: dbError,
});
throw appError;