1
0

add context to directive errors (#513)

This commit is contained in:
Ian Shehadeh 2023-09-05 04:24:50 -04:00 committed by GitHub
parent b936a435ab
commit 9188a772b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,25 @@ import {
templateDirectiveRenderer,
} 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 =
/<!--\s*#(use|use-verbose|include|eval|query)\s+(.*?)-->/i;
@ -68,12 +87,19 @@ export async function renderDirectives(
pageMeta: PageMeta,
directiveTree: ParseTree,
): Promise<string> {
const replacementText = await directiveDispatcher(pageMeta, directiveTree, {
use: templateDirectiveRenderer,
include: templateDirectiveRenderer,
query: queryDirectiveRenderer,
eval: evalDirectiveRenderer,
});
return cleanTemplateInstantiations(replacementText);
try {
const replacementText = await directiveDispatcher(pageMeta, directiveTree, {
use: templateDirectiveRenderer,
include: templateDirectiveRenderer,
query: queryDirectiveRenderer,
eval: evalDirectiveRenderer,
});
return cleanTemplateInstantiations(replacementText);
} catch (e) {
throw new RenderDirectiveError(
pageMeta,
renderToText(directiveTree.children![0].children![1]).trim(),
e,
);
}
}