Skip to content

Commit

Permalink
Better error messaging for #3286 (applies to all filter/shortcode def…
Browse files Browse the repository at this point in the history
…initions)
  • Loading branch information
zachleat committed Jul 1, 2024
1 parent 2d4a942 commit 1c2fbbd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,37 @@ class Nunjucks extends TemplateEngine {
let [context, ...argArray] = args;

if (isAsync) {
shortcodeFn
.call(Nunjucks.normalizeContext(context), ...argArray)
.then(function (returnValue) {
resolve(null, new NunjucksLib.runtime.SafeString("" + returnValue));
})
.catch(function (e) {
resolve(
new EleventyShortcodeError(
`Error with Nunjucks shortcode \`${shortcodeName}\`${EleventyErrorUtil.convertErrorToString(
e,
)}`,
),
null,
try {
let ret = shortcodeFn.call(Nunjucks.normalizeContext(context), ...argArray);

// #3286 error messaging when the shortcode is not a promise
if (!ret?.then) {
throw new EleventyShortcodeError(
`Error with Nunjucks shortcode \`${shortcodeName}\`: it was defined as asynchronous but was actually synchronous. This is important for Nunjucks.`,
);
});
}

ret
.then(function (returnValue) {
resolve(null, new NunjucksLib.runtime.SafeString("" + returnValue));
})
.catch(function (e) {
resolve(
new EleventyShortcodeError(
`Error with Nunjucks shortcode \`${shortcodeName}\`${EleventyErrorUtil.convertErrorToString(
e,
)}`,
),
null,
);
});
} catch (e) {
throw new EleventyShortcodeError(
`Error with Nunjucks shortcode \`${shortcodeName}\`${EleventyErrorUtil.convertErrorToString(
e,
)}`,
);
}
} else {
try {
let ret = shortcodeFn.call(Nunjucks.normalizeContext(context), ...argArray);
Expand Down
4 changes: 4 additions & 0 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ class UserConfig {
debug(`Adding new ${description} "%o" via \`%o(%o)\``, name, fnName, originalName);
}

if (typeof callback !== "function") {
throw new Error(`Invalid definition for "${name}" ${description}.`);
}

target[name] = this.benchmarks.config.add(`"${name}" ${description}`, callback);
}

Expand Down

0 comments on commit 1c2fbbd

Please sign in to comment.