Skip to content

Commit

Permalink
Better error messaging for missing layout files.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 27, 2024
1 parent 6c760ad commit 8453aea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
26 changes: 7 additions & 19 deletions src/TemplateLayoutPathResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class TemplateLayoutPathResolver {

this.eleventyConfig = eleventyConfig;
this.originalPath = path;
this.originalDisplayPath =
TemplatePath.join(this.layoutsDir, this.originalPath) +
` (via \`layout: ${this.originalPath}\`)`; // for error messaging
this.path = path;
this.aliases = {};
this.extensionMap = extensionMap;
Expand Down Expand Up @@ -59,14 +62,8 @@ class TemplateLayoutPathResolver {
init() {
// we might be able to move this into the constructor?
this.aliases = Object.assign({}, this.config.layoutAliases, this.aliases);
// debug("Current layout aliases: %o", this.aliases);

if (this.path in this.aliases) {
// debug(
// "Substituting layout: %o maps to %o",
// this.path,
// this.aliases[this.path]
// );

if (this.aliases[this.path]) {
this.path = this.aliases[this.path];
}

Expand All @@ -92,7 +89,7 @@ class TemplateLayoutPathResolver {
getFileName() {
if (!this.filename) {
throw new Error(
`You’re trying to use a layout that does not exist: ${this.originalPath}${this.filename ? ` (${this.filename})` : ""}`,
`You’re trying to use a layout that does not exist: ${this.originalDisplayPath}`,
);
}

Expand All @@ -102,23 +99,14 @@ class TemplateLayoutPathResolver {
getFullPath() {
if (!this.filename) {
throw new Error(
`You’re trying to use a layout that does not exist: ${this.originalPath}${this.filename ? ` (${this.filename})` : ""}`,
`You’re trying to use a layout that does not exist: ${this.originalDisplayPath}`,
);
}

return this.fullPath;
}

findFileName() {
if (!fs.existsSync(this.layoutsDir)) {
throw Error(
"TemplateLayoutPathResolver directory does not exist for " +
this.path +
": " +
this.layoutsDir,
);
}

for (let filename of this.extensionMap.getFileList(this.path)) {
// TODO async
if (fs.existsSync(TemplatePath.join(this.layoutsDir, filename))) {
Expand Down
2 changes: 1 addition & 1 deletion test/TemplateLayoutPathResolverTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ test("Layout (uses empty string layouts folder) no template resolution", async (
t.throws(() => {
res.getFileName();
}, {
message: `You’re trying to use a layout that does not exist: layoutsemptystring`
message: `You’re trying to use a layout that does not exist: test/stubs/layoutsemptystring (via \`layout: layoutsemptystring\`)`
});
});

Expand Down

0 comments on commit 8453aea

Please sign in to comment.