Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better logging if writing a tag/catetory/author page fails #3266

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 75 additions & 39 deletions pelican/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,57 +560,93 @@ def generate_tags(self, write):
tag_template = self.get_template("tag")
for tag, articles in self.tags.items():
dates = [article for article in self.dates if article in articles]
write(
tag.save_as,
tag_template,
self.context,
tag=tag,
url=tag.url,
articles=articles,
dates=dates,
template_name="tag",
blog=True,
page_name=tag.page_name,
all_articles=self.articles,
)
try:
write(
tag.save_as,
tag_template,
self.context,
tag=tag,
url=tag.url,
articles=articles,
dates=dates,
template_name="tag",
blog=True,
page_name=tag.page_name,
all_articles=self.articles,
)
except RuntimeError:
if not tag.slug:
logger.info(
'Tag "%s" has an invalid slug; skipping writing tag page...',
tag,
extra={"limit_msg": "Further tags with invalid slugs."},
)
continue
Comment on lines +578 to +584
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With empty slugs already generating warnings, is this special case necessary anymore? Why not just do the else block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's helpful to still keep this, because it makes it very explicity what Pelican is (or isn't doing), and it's not entirely obvious that an invalid slug would keep the page from being generated.

Also, it mirrors the logging for all the pages that are written.

else:
logger.error('Failed to write Tag page for "%s".', tag)
raise

def generate_categories(self, write):
"""Generate category pages."""
category_template = self.get_template("category")
for cat, articles in self.categories:
dates = [article for article in self.dates if article in articles]
write(
cat.save_as,
category_template,
self.context,
url=cat.url,
category=cat,
articles=articles,
dates=dates,
template_name="category",
blog=True,
page_name=cat.page_name,
all_articles=self.articles,
)
try:
write(
cat.save_as,
category_template,
self.context,
url=cat.url,
category=cat,
articles=articles,
dates=dates,
template_name="category",
blog=True,
page_name=cat.page_name,
all_articles=self.articles,
)
except RuntimeError:
if not cat.slug:
logger.info(
'Category "%s" has an invalid slug; skipping writing category page...',
cat,
extra={"limit_msg": "Further categories with invalid slugs."},
)
continue
else:
logger.error('Failed to write Category page for "%s".', cat)
raise

def generate_authors(self, write):
"""Generate Author pages."""
author_template = self.get_template("author")
for aut, articles in self.authors:
dates = [article for article in self.dates if article in articles]
write(
aut.save_as,
author_template,
self.context,
url=aut.url,
author=aut,
articles=articles,
dates=dates,
template_name="author",
blog=True,
page_name=aut.page_name,
all_articles=self.articles,
)
try:
write(
aut.save_as,
author_template,
self.context,
url=aut.url,
author=aut,
articles=articles,
dates=dates,
template_name="author",
blog=True,
page_name=aut.page_name,
all_articles=self.articles,
)
except RuntimeError:
if not aut.slug:
logger.info(
'Author "%s" has an invalid slug; skipping writing author page...',
aut,
extra={"limit_msg": "Further authors with invalid slugs."},
)
continue
else:
logger.error('Failed to write Author page for "%s".', aut)
raise

def generate_drafts(self, write):
"""Generate drafts pages."""
Expand Down
9 changes: 8 additions & 1 deletion pelican/urlwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ def slug(self):
preserve_case=preserve_case,
use_unicode=self.settings.get("SLUGIFY_USE_UNICODE", False),
)
if not self._slug:
logger.warning(
'Unable to generate valid slug for %s "%s".',
self.__class__.__name__,
self.name,
extra={"limit_msg": "Other invalid slugs."},
)
return self._slug

@slug.setter
def slug(self, slug):
# if slug is expliticly set, changing name won't alter slug
# if slug is explicitly set, changing name won't alter slug
self._slug_from_name = False
self._slug = slug

Expand Down
2 changes: 1 addition & 1 deletion pelican/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def slugify(value, regex_subs=(), preserve_case=False, use_unicode=False):
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.

Took from Django sources.
Taken from Django sources.

For a set of sensible default regex substitutions to pass to regex_subs
look into pelican.settings.DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS'].
Expand Down