add context to directive errors (#513)
This commit is contained in:
parent
b936a435ab
commit
9188a772b6
@ -8,6 +8,25 @@ import {
|
|||||||
templateDirectiveRenderer,
|
templateDirectiveRenderer,
|
||||||
} from "./template_directive.ts";
|
} from "./template_directive.ts";
|
||||||
|
|
||||||
|
/** An error that occurs while a directive is being rendered.
|
||||||
|
* Mostly annotates the underlying error with page metadata.
|
||||||
|
*/
|
||||||
|
export class RenderDirectiveError extends Error {
|
||||||
|
pageMeta: PageMeta;
|
||||||
|
directive: string;
|
||||||
|
cause: Error;
|
||||||
|
|
||||||
|
constructor(pageMeta: PageMeta, directive: string, cause: Error) {
|
||||||
|
super(`In directive "${directive}" from "${pageMeta.name}": ${cause}`, {
|
||||||
|
cause: cause,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.pageMeta = pageMeta;
|
||||||
|
this.directive = directive;
|
||||||
|
this.cause = cause;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const directiveStartRegex =
|
export const directiveStartRegex =
|
||||||
/<!--\s*#(use|use-verbose|include|eval|query)\s+(.*?)-->/i;
|
/<!--\s*#(use|use-verbose|include|eval|query)\s+(.*?)-->/i;
|
||||||
|
|
||||||
@ -68,12 +87,19 @@ export async function renderDirectives(
|
|||||||
pageMeta: PageMeta,
|
pageMeta: PageMeta,
|
||||||
directiveTree: ParseTree,
|
directiveTree: ParseTree,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const replacementText = await directiveDispatcher(pageMeta, directiveTree, {
|
try {
|
||||||
use: templateDirectiveRenderer,
|
const replacementText = await directiveDispatcher(pageMeta, directiveTree, {
|
||||||
include: templateDirectiveRenderer,
|
use: templateDirectiveRenderer,
|
||||||
query: queryDirectiveRenderer,
|
include: templateDirectiveRenderer,
|
||||||
eval: evalDirectiveRenderer,
|
query: queryDirectiveRenderer,
|
||||||
});
|
eval: evalDirectiveRenderer,
|
||||||
|
});
|
||||||
return cleanTemplateInstantiations(replacementText);
|
return cleanTemplateInstantiations(replacementText);
|
||||||
|
} catch (e) {
|
||||||
|
throw new RenderDirectiveError(
|
||||||
|
pageMeta,
|
||||||
|
renderToText(directiveTree.children![0].children![1]).trim(),
|
||||||
|
e,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user