Files
suaveplan/packages/foundation/data/templates/src/errors/template-error.ts
2026-05-27 23:47:53 +01:00

76 lines
2.6 KiB
TypeScript

import { SuaveplanError, type SuaveplanErrorOptions } from "@suaveplan/error";
import { TEMPLATE_CODES, type TemplateCode } from "./error-codes.js";
export type { TemplateCode };
export { TEMPLATE_CODES };
// ─── Base ───────────────────────────────────────────────────────────────────
/**
* Base error class for all template engine failures.
*
* Carries the original `template` string and the `renderContext` object so
* callers can log rich diagnostics without re-parsing the call-stack.
*/
export class TemplateError extends SuaveplanError<TemplateCode> {
public readonly template: string;
public readonly renderContext: Record<string, unknown>;
constructor(
code: TemplateCode,
messageKey: string,
template: string,
renderContext: Record<string, unknown>,
options?: SuaveplanErrorOptions
) {
super(code, messageKey, options);
this.template = template;
this.renderContext = renderContext;
}
}
// ─── Plugin error ────────────────────────────────────────────────────────────
/**
* Emitted when a plugin's `transform()` throws or returns a rejected Promise.
*
* Adds `pluginName` and `matchedText` for quick diagnostics.
*/
export class PluginError extends SuaveplanError<TemplateCode> {
public readonly pluginName: string;
public readonly match: string;
constructor(
message: string,
pluginName: string,
match: string,
cause?: Error
) {
super(
TEMPLATE_CODES.PLUGIN_ERROR,
message,
cause ? { cause } : undefined
);
this.pluginName = pluginName;
this.match = match;
}
}
// ─── Max depth error ─────────────────────────────────────────────────────────
/**
* Thrown when recursive template nesting exceeds `TemplateEngineOptions.maxDepth`.
*
* Includes `depth` (current) and `maxDepth` (limit) for diagnostics.
*/
export class MaxDepthError extends SuaveplanError<TemplateCode> {
public readonly depth: number;
public readonly maxDepth: number;
constructor(message: string, depth: number, maxDepth: number) {
super(TEMPLATE_CODES.MAX_DEPTH, message);
this.depth = depth;
this.maxDepth = maxDepth;
}
}