From f92092f4ac192d34bdb1a3ea2fa9bc587ed7cfe6 Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Thu, 17 Apr 2025 10:20:03 +0200 Subject: [PATCH] Improve error message in case of invalid path Previously, if blog authors didn't set the path key in the front matter, the error message from Zola was: Filter `nth` received an incorrect type for arg `n`: got `-2` but expected a usize This is misleading, as it doesn't point out the cause of the problem. The new error message makes authors aware of the required path format. --- templates/feed.xml | 1 + templates/index.html | 1 + templates/page.html | 1 + templates/releases.html | 1 + 4 files changed, 4 insertions(+) diff --git a/templates/feed.xml b/templates/feed.xml index dc7b356b8..ae19e1e8f 100644 --- a/templates/feed.xml +++ b/templates/feed.xml @@ -20,6 +20,7 @@ {{ macros::escape_hbs(input=page.title) | safe }} {%- set num_comps = page.components | length %} + {%- if num_comps < 4 %}{{ throw(message="Missing date in 'path' key, required format: '[inside-rust/]YYYY/MM/DD/slug-of-your-choice'") }}{% endif %} {%- set year = page.components | nth(n=num_comps - 4) %} {%- set month = page.components | nth(n=num_comps - 3) %} {%- set day = page.components | nth(n=num_comps - 2) %} diff --git a/templates/index.html b/templates/index.html index 786910b4e..f6c79456c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -27,6 +27,7 @@ {%- set rev_pages = section.pages | reverse %} {%- for page in rev_pages %} {%- set num_comps = page.components | length %} + {%- if num_comps < 4 %}{{ throw(message="Missing date in 'path' key, required format: '[inside-rust/]YYYY/MM/DD/slug-of-your-choice'") }}{% endif %} {%- set year = page.components | nth(n=num_comps - 4) | int %} {%- set month = page.components | nth(n=num_comps - 3) | int %} {%- set day = page.components | nth(n=num_comps - 2) | int %} diff --git a/templates/page.html b/templates/page.html index 76091ddf0..ae6c8e267 100644 --- a/templates/page.html +++ b/templates/page.html @@ -2,6 +2,7 @@ {% extends "layout.html" -%} {% block page -%} {% set num_comps = page.components | length -%} +{% if num_comps < 4 %}{{ throw(message="Missing date in 'path' key, required format: '[inside-rust/]YYYY/MM/DD/slug-of-your-choice'") }}{% endif -%} {% set year = page.components | nth(n=num_comps - 4) | int -%} {% set month = page.components | nth(n=num_comps - 3) | int -%} {% set day = page.components | nth(n=num_comps - 2) | int -%} diff --git a/templates/releases.html b/templates/releases.html index fcfaa100b..5cc1d55c2 100644 --- a/templates/releases.html +++ b/templates/releases.html @@ -26,6 +26,7 @@ {%- set rev_pages = section.pages | reverse %} {%- for page in rev_pages %} {%- set num_comps = page.components | length %} + {%- if num_comps < 4 %}{{ throw(message="Missing date in 'path' key, required format: '[inside-rust/]YYYY/MM/DD/slug-of-your-choice'") }}{% endif %} {%- set year = page.components | nth(n=num_comps - 4) | int %} {%- set month = page.components | nth(n=num_comps - 3) | int %} {%- set day = page.components | nth(n=num_comps - 2) | int %}