CHAN.RUN
function defineError<N>(name, options?): NamedFaultErrorClass<N>;Defined in: src/define-error.ts
Define a reusable typed error class.
const NotFoundError = defineError("NotFoundError");
const ValidationError = defineError("ValidationError", { code: "VALIDATION" });| Type Parameter |
|---|
N extends string |
| Parameter | Type |
|---|---|
name | N |
options? | DefineErrorOptions |
// errors/auth.ts
export const UnauthorizedError = defineError("UnauthorizedError");
export const TokenExpiredError = defineError("TokenExpiredError");
export const ForbiddenError = defineError("ForbiddenError");
// errors/billing.ts
export const PaymentFailedError = defineError("PaymentFailedError");
export const QuotaExceededError = defineError("QuotaExceededError");const RateLimitError = defineError("RateLimitError", { code: "RATE_LIMITED" });
// In an API handler:
// err.name → "RateLimitError" (for matching)
// err.code → "RATE_LIMITED" (for API response payloads)class HttpError extends Error {
status = 500;
}
const NotFoundError = defineError("NotFoundError", { base: HttpError });
const err = new NotFoundError("gone");
err instanceof HttpError; // true
err.status; // 500const InvalidEmailError = defineError("InvalidEmailError", { code: "INVALID_EMAIL" });
const RequiredFieldError = defineError("RequiredFieldError", { code: "REQUIRED" });
function validateEmail(value: string) {
if (!value) fault(RequiredFieldError, "Email is required");
if (!value.includes("@")) fault(InvalidEmailError, "Not a valid email");
}const DbError = defineError("DbError");
const err = new DbError("connection lost");
err instanceof Error; // true
err instanceof DbError; // true
err.name; // "DbError"
err.message; // "connection lost"
err.isFault; // true
err.stack; // full stack trace