muddle/core/errors.ts
2026-10-23 08:53:07 -04:00

46 lines
1.4 KiB
TypeScript

/** Base error for every failure raised by the framework. */
export class MuddleError extends Error {
constructor(message: string, options?: { cause?: unknown }) {
super(message, options);
this.name = "MuddleError";
}
}
/** Raised when a dependency cannot be resolved from the current module graph. */
export class UnknownProviderError extends MuddleError {
constructor(token: unknown) {
super(`Cannot resolve dependency: ${describeToken(token)}`);
this.name = "UnknownProviderError";
}
}
/** Raised when a request does not match any route. */
export class RouteNotFoundError extends MuddleError {
constructor(method: string, path: string) {
super(`No route matches ${method} ${path}`);
this.name = "RouteNotFoundError";
}
}
/** Raised while compiling or rendering a template. */
export class TemplateError extends MuddleError {
constructor(message: string, options?: { cause?: unknown }) {
super(message, options);
this.name = "TemplateError";
}
}
/** Raised when the ORM cannot find a table definition. */
export class SchemaError extends MuddleError {
constructor(message: string) {
super(message);
this.name = "SchemaError";
}
}
/** Human readable representation of an injection token. */
export function describeToken(token: unknown): string {
if (typeof token === "function") return token.name || "anonymous class";
if (typeof token === "symbol") return token.toString();
return String(token);
}