Skip to content

Commit

Permalink
Fixes #3295
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 20, 2024
1 parent 5aa1e02 commit 6d99dd5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/Engines/Liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ class Liquid extends TemplateEngine {
return options;
}

static wrapFilter(fn) {
static wrapFilter(name, fn) {
return function (...args) {
if (this.context && "get" in this.context) {
const exposedProperties = ["page", "eleventy"];
for (const property of exposedProperties) {
Object.defineProperty(this, property, {
for (let propertyName of ["page", "eleventy"]) {
Object.defineProperty(this, propertyName, {
configurable: true,
enumerable: true,
get: () => this.context.get([property]),
get: () => this.context.get([propertyName]),
});
}
}

// We *don’t* wrap this in an EleventyFilterError because Liquid has a better error message with line/column information in the template
return fn.call(this, ...args);
};
}
Expand Down Expand Up @@ -96,7 +96,7 @@ class Liquid extends TemplateEngine {
}

addFilter(name, filter) {
this.liquidLib.registerFilter(name, Liquid.wrapFilter(filter));
this.liquidLib.registerFilter(name, Liquid.wrapFilter(name, filter));
}

addTag(name, tagFn) {
Expand Down
23 changes: 17 additions & 6 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { TemplatePath } from "@11ty/eleventy-utils";

import TemplateEngine from "./TemplateEngine.js";
import EleventyErrorUtil from "../Errors/EleventyErrorUtil.js";
import EleventyBaseError from "../Errors/EleventyBaseError.js";
import EleventyShortcodeError from "../Errors/EleventyShortcodeError.js";
import EventBusUtil from "../Util/EventBusUtil.js";

class EleventyFilterError extends EleventyBaseError {}

class Nunjucks extends TemplateEngine {
constructor(name, eleventyConfig) {
super(name, eleventyConfig);
Expand Down Expand Up @@ -50,7 +53,7 @@ class Nunjucks extends TemplateEngine {
paths.add(TemplatePath.getWorkingDir());

// Filter out undefined paths
let fsLoader = new NunjucksLib.FileSystemLoader(Array.from(paths).filter((entry) => entry));
let fsLoader = new NunjucksLib.FileSystemLoader(Array.from(paths).filter(Boolean));

this.njkEnv = new NunjucksLib.Environment(fsLoader, this.nunjucksEnvironmentOptions);
}
Expand Down Expand Up @@ -92,11 +95,11 @@ class Nunjucks extends TemplateEngine {

addFilters(filters, isAsync) {
for (let name in filters) {
this.njkEnv.addFilter(name, Nunjucks.wrapFilter(filters[name]), isAsync);
this.njkEnv.addFilter(name, Nunjucks.wrapFilter(name, filters[name]), isAsync);
}
}

static wrapFilter(fn) {
static wrapFilter(name, fn) {
return function (...args) {
if (this.ctx && this.ctx.page) {
this.page = this.ctx.page;
Expand All @@ -105,7 +108,14 @@ class Nunjucks extends TemplateEngine {
this.eleventy = this.ctx.eleventy;
}

return fn.call(this, ...args);
try {
return fn.call(this, ...args);
} catch (e) {
throw new EleventyFilterError(
`Error in Nunjucks filter \`${name}\`${this.page ? ` (${this.page.inputPath})` : ""}${EleventyErrorUtil.convertErrorToString(e)}`,
e,
);
}
};
}

Expand Down Expand Up @@ -402,12 +412,13 @@ class Nunjucks extends TemplateEngine {
async compile(str, inputPath) {
let tmpl;

// *All* templates are precompiled to avoid runtime eval
if (this._usingPrecompiled) {
tmpl = this.njkEnv.getTemplate(str, true);
} else if (!inputPath || inputPath === "njk" || inputPath === "md") {
tmpl = new NunjucksLib.Template(str, this.njkEnv, null, true);
tmpl = new NunjucksLib.Template(str, this.njkEnv, null, false);
} else {
tmpl = new NunjucksLib.Template(str, this.njkEnv, inputPath, true);
tmpl = new NunjucksLib.Template(str, this.njkEnv, inputPath, false);
}

return async function (data) {
Expand Down
15 changes: 9 additions & 6 deletions src/Errors/EleventyErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,28 @@ class EleventyErrorHandler {
this.log(e, "error", undefined, true);
}

//https://nodejs.org/api/process.html
log(e, type = "log", chalkColor = "", forceToConsole = false) {
let errorCount = 0;
static getTotalErrorCount(e) {
let totalErrorCount = 0;
let errorCountRef = e;
while (errorCountRef) {
errorCount++;
totalErrorCount++;
errorCountRef = errorCountRef.originalError;
}
return totalErrorCount;
}

//https://nodejs.org/api/process.html
log(e, type = "log", chalkColor = "", forceToConsole = false) {
let totalErrorCount = EleventyErrorHandler.getTotalErrorCount(e);
let ref = e;
let index = 1;
while (ref) {
let nextRef = ref.originalError;
if (!nextRef && EleventyErrorUtil.hasEmbeddedError(ref.message)) {
nextRef = EleventyErrorUtil.deconvertErrorToObject(ref);
}

this.logger.message(
`${errorCount > 1 ? `${index}. ` : ""}${(
`${totalErrorCount > 1 ? `${index}. ` : ""}${(
EleventyErrorUtil.cleanMessage(ref.message) || "(No error message provided)"
).trim()} (via ${ref.name})`,
type,
Expand Down
8 changes: 3 additions & 5 deletions src/Errors/EleventyErrorUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ class EleventyErrorUtil {
return false;
}

return msg.indexOf(EleventyErrorUtil.prefix) > -1 && msg.indexOf(EleventyErrorUtil.suffix) > -1;
return msg.includes(EleventyErrorUtil.prefix) && msg.includes(EleventyErrorUtil.suffix);
}

static cleanMessage(msg) {
if (!msg) {
return "";
}

if (!EleventyErrorUtil.hasEmbeddedError(msg)) {
return "" + msg;
}

return msg.slice(
0,
msg.indexOf(EleventyErrorUtil.prefix) < 0 ? 0 : msg.indexOf(EleventyErrorUtil.prefix),
);
return msg.slice(0, Math.max(0, msg.indexOf(EleventyErrorUtil.prefix)));
}

static deconvertErrorToObject(error) {
Expand Down

0 comments on commit 6d99dd5

Please sign in to comment.