getzola/themes

Handling errors in theme parsing without halting entire process

welpo opened this issue · 2 comments

welpo commented
TL;DR

The generate_docs.py script halts when a theme's TOML is malformed, preventing further processing of other themes. Proposing to capture and log these errors instead of stopping the entire process, and then display a summary at the end.

Description

While using the generate_docs.py script, the process halts entirely when encountering a theme with a malformed TOML, such as duplicate keys. This is currently happening with the zola-henry theme: sirodoht/zola-henry#6

Problem

If a theme's TOML has a duplicate key or any other TOML error, the script halts entirely. This means that a single theme with a malformed TOML can stop all subsequent themes from being processed.

While the current behavior makes it evident which theme has an error due to the halt, this approach may not be ideal if the goal is to update/process all themes that have no errors.

Proposed solution

  1. Instead of halting the entire script upon encountering an error, capture the error and continue processing the next theme.
  2. Append the error to a list or log.
  3. At the conclusion of the script's run, display a summary of all themes that had errors.

Advantages

  • Ensures that one malformed theme doesn't prevent other themes from being processed.
  • Provides a clear summary of themes with errors, presented at the end for easy reference.

Sample code changes

# Global scope
errors = []

# Inside Theme class constructor
try:
    with open(os.path.join(self.path, "theme.toml")) as f:
        self.metadata = toml.load(f)
except Exception as e:
    error_message = f"Error parsing TOML for theme {self.name}. {str(e)}"
    errors.append(error_message)
    self.metadata = None
    return

# At the end of the script
if errors:
    print("\n\n---- ERRORS ----")
    for error in errors:
        print(error)

It might also be a good idea to add a summary line at the end of the script that tells how many themes were successfully processed and how many had errors:

print(f"Processed {successful_count} themes successfully. Encountered errors in {error_count} themes.")

I'd be happy to create a PR with the suggested changes.

Keats commented

Yeah that would be fine as a PR

welpo commented

Implemented in #120