Skip to content

Commit 13ac77a

Browse files
committed
Improve error logging
Example output when specifying a non-existent template file: ``` ❯ pdfbaker bake examples/examples.yml INFO: Processing document "minimal" from /home/danny/src/pdfbaker/examples/minimal... INFO: Processing document "regular" from /home/danny/src/pdfbaker/examples/regular... ERROR: Failed to process document 'regular': 'introDOESNOTEXIST.svg.j2' not found in search path: '/home/danny/src/pdfbaker/examples/regular/templates' INFO: Processing document "variants" from /home/danny/src/pdfbaker/examples/variants... INFO: Processing variant "Basic"... INFO: Processing variant "Premium"... INFO: Processing variant "Enterprise"... INFO: Processing document "custom_processing" from /home/danny/src/pdfbaker/examples/custom_processing... INFO: Done. INFO: PDF files created in /home/danny/src/pdfbaker/examples/dist WARNING: There were errors. ❯ pdfbaker bake -q examples/examples.yml ERROR: Failed to process document 'regular': 'introDOESNOTEXIST.svg.j2' not found in search path: '/home/danny/src/pdfbaker/examples/regular/templates' ~/src/pdfbaker  on main ⇡1 !4  ```
1 parent c82faa8 commit 13ac77a

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/pdfbaker/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def bake(config_file: Path, debug: bool, verbose: bool, quiet: bool) -> int:
4444
try:
4545
baker = PDFBaker(config_file)
4646
baker.bake(debug=debug)
47-
logger.info("Done.")
4847
return 0
4948
except PDFBakeError as exc:
5049
logger.error(str(exc))

src/pdfbaker/baker.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def bake(self, debug: bool = False) -> None:
5555
debug: If True, keep build files for debugging
5656
"""
5757
document_paths = self._get_document_paths(self.config.get("documents", []))
58+
pdfs_created: list[str] = []
59+
failed_docs: list[tuple[str, str]] = []
5860

5961
for doc_name, doc_path in document_paths.items():
6062
doc = PDFBakerDocument(
@@ -63,11 +65,26 @@ def bake(self, debug: bool = False) -> None:
6365
baker=self,
6466
)
6567
doc.setup_directories()
66-
doc.process_document()
68+
pdf_file, error_message = doc.process_document()
69+
if pdf_file is not None:
70+
pdfs_created.append(pdf_file)
71+
else:
72+
self.error(
73+
"Failed to process document '%s': %s", doc_name, error_message
74+
)
75+
failed_docs.append((doc_name, error_message))
6776

6877
if not debug:
6978
self._teardown_build_directories(list(document_paths.keys()))
7079

80+
self.info("Done.")
81+
if pdfs_created:
82+
self.info("PDF files created in %s", self.dist_dir.resolve())
83+
else:
84+
self.warning("No PDF files created.")
85+
if failed_docs:
86+
self.warning("There were errors.")
87+
7188
def _load_config(self, config_file: Path) -> dict[str, Any]:
7289
"""Load configuration from YAML file."""
7390
try:

src/pdfbaker/document.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from typing import Any
77

8+
import jinja2
89
import yaml
910

1011
from .common import (
@@ -150,16 +151,30 @@ def setup_directories(self) -> None:
150151
if os.path.isfile(file_path):
151152
os.remove(file_path)
152153

153-
def process_document(self) -> None:
154-
"""Process the document - use custom bake module if it exists."""
154+
def process_document(self) -> tuple[Path | None, str | None]:
155+
"""Process the document - use custom bake module if it exists.
156+
157+
Returns:
158+
Tuple of (pdf_file, error_message) where:
159+
- pdf_file is the path to the created PDF, or None if creation failed
160+
- error_message is a string describing the error, or None if successful
161+
"""
155162
self.baker.info('Processing document "%s" from %s...', self.name, self.doc_dir)
156163

157164
# Try to load custom bake module
158165
bake_path = self.doc_dir / "bake.py"
159166
if bake_path.exists():
160-
self._process_with_custom_bake(bake_path)
167+
try:
168+
self._process_with_custom_bake(bake_path)
169+
return self.dist_dir / f"{self.config['filename']}.pdf", None
170+
except PDFBakeError as exc:
171+
return None, str(exc)
161172
else:
162-
self.process()
173+
try:
174+
self.process()
175+
return self.dist_dir / f"{self.config['filename']}.pdf", None
176+
except (PDFBakeError, jinja2.exceptions.TemplateError) as exc:
177+
return None, str(exc)
163178

164179
def process(self) -> None:
165180
"""Process document using standard processing."""
@@ -168,7 +183,7 @@ def process(self) -> None:
168183
if "variants" in self.config:
169184
# Multiple PDF documents
170185
for variant in self.config["variants"]:
171-
self.baker.info('Processing variant "%s"', variant["name"])
186+
self.baker.info('Processing variant "%s"...', variant["name"])
172187
variant_config = deep_merge(doc_config, variant)
173188
variant_config["variant"] = variant
174189
variant_config = resolve_config(variant_config)

0 commit comments

Comments
 (0)