Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.
Python agent-skillsai-agentsbook-to-skillcontext-engineeringdocument-processingedtechknowledge-baseknowledge-managementllmpdf-to-markdownragself-studystudy-tools
fix: log unexpected exceptions in parsers to stderr (#50)
Adithi Hebbar Mef29388bd928
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7f37b3d..5e21e86 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Fixed+- Log unexpected parser exceptions to stderr instead of silently returning `None`+ corrupt files and encoding errors are now visible while the fallback chain continues+ unchanged.+### Added- **`pyproject.toml`** declaring `requires-python = ">=3.9"` and centralizing theruff/pytest config so `ruff check .` and `pytest` locally match CI withoutdiff --git a/scripts/extractor/parsers/calibre.py b/scripts/extractor/parsers/calibre.pyindex 96a6a14..d43766a 100644--- a/scripts/extractor/parsers/calibre.py+++ b/scripts/extractor/parsers/calibre.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport shutilimport subprocess+import sysfrom extractor.config import OUTPUT_DIR@@ -18,6 +19,6 @@ def extract_with_ebook_convert(input_path: str) -> str | None:text = output_path.read_text(encoding="utf-8", errors="replace")if text.strip():return text- except Exception:- pass+ except Exception as e:+ print(f" [warn] extract_with_ebook_convert failed: {type(e).__name__}: {e}", file=sys.stderr)return Nonediff --git a/scripts/extractor/parsers/docx.py b/scripts/extractor/parsers/docx.pyindex e6bf9dc..c570b27 100644--- a/scripts/extractor/parsers/docx.py+++ b/scripts/extractor/parsers/docx.py@@ -1,6 +1,7 @@from __future__ import annotationsimport zipfile+import sysfrom extractor.exceptions import ExtractionError@@ -17,10 +18,10 @@ def extract_docx_with_python_docx(docx_path: str) -> str | None:return "\n".join(parts)except ImportError:return None- except Exception:+ except Exception as e:+ print(f" [warn] extract_docx_with_python_docx failed: {type(e).__name__}: {e}", file=sys.stderr)return None-def extract_docx_with_zipfile(docx_path: str) -> str | None:try:import xml.etree.ElementTree as ET@@ -35,7 +36,8 @@ def extract_docx_with_zipfile(docx_path: str) -> str | None:if texts:parts.append("".join(texts))return "\n".join(parts) if parts else None- except Exception:+ except Exception as e:+ print(f" [warn] extract_docx_with_zipfile failed: {type(e).__name__}: {e}", file=sys.stderr)return Nonediff --git a/scripts/extractor/parsers/epub.py b/scripts/extractor/parsers/epub.pyindex f905c23..f60c5d2 100644--- a/scripts/extractor/parsers/epub.py+++ b/scripts/extractor/parsers/epub.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport posixpathimport reimport zipfile+import sysfrom extractor.parsers.html import _HTMLTextExtractor@@ -20,7 +21,8 @@ def extract_with_ebooklib(epub_path: str) -> str | None:return "\n\n".join(parts)except ImportError:return None- except Exception:+ except Exception as e:+ print(f" [warn] extract_with_ebooklib failed: {type(e).__name__}: {e}", file=sys.stderr)return None@@ -80,7 +82,8 @@ def extract_with_zipfile(epub_path: str) -> str | None:except Exception:continuereturn "\n\n".join(parts) if parts else None- except Exception:+ except Exception as e:+ print(f" [warn] extract_with_zipfile failed: {type(e).__name__}: {e}", file=sys.stderr)return Nonediff --git a/scripts/extractor/parsers/pdf.py b/scripts/extractor/parsers/pdf.pyindex 614fa7a..3ea29c0 100644--- a/scripts/extractor/parsers/pdf.py+++ b/scripts/extractor/parsers/pdf.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport shutilimport subprocess+import sysdef extract_with_pdftotext(pdf_path: str) -> str | None:@@ -14,8 +15,8 @@ def extract_with_pdftotext(pdf_path: str) -> str | None:)if result.returncode == 0 and result.stdout.strip():return result.stdout- except Exception:- pass+ except Exception as e:+ print(f" [warn] extract_with_pdftotext failed: {type(e).__name__}: {e}", file=sys.stderr)return None@@ -33,7 +34,8 @@ def extract_with_pypdf2(pdf_path: str) -> str | None:return "\n".join(text_parts)except ImportError:return None- except Exception:+ except Exception as e:+ print(f" [warn] extract_with_pypdf2 failed: {type(e).__name__}: {e}", file=sys.stderr)return None@@ -43,7 +45,8 @@ def extract_with_pdfminer(pdf_path: str) -> str | None:return extract_text(pdf_path)except ImportError:return None- except Exception:+ except Exception as e:+ print(f" [warn] extract_with_pdfminer failed: {type(e).__name__}: {e}", file=sys.stderr)return None@@ -68,7 +71,8 @@ def extract_with_docling(pdf_path: str) -> str | None:return result.document.export_to_markdown()except ImportError:return None- except Exception:+ except Exception as e:+ print(f" [warn] extract_with_docling failed: {type(e).__name__}: {e}", file=sys.stderr)return Nonediff --git a/scripts/extractor/parsers/rtf.py b/scripts/extractor/parsers/rtf.pyindex 1df208b..06cab49 100644--- a/scripts/extractor/parsers/rtf.py+++ b/scripts/extractor/parsers/rtf.py@@ -1,5 +1,6 @@import htmlimport re+import sysfrom extractor.parsers.text import read_text_filefrom extractor.exceptions import ExtractionError@@ -25,7 +26,7 @@ def extract_rtf(rtf_path: str) -> tuple[str, str]:return text, "striprtf"except ImportError:pass- except Exception:- pass+ except Exception as e:+ print(f" [warn] extract_rtf/striprtf failed: {type(e).__name__}: {e}", file=sys.stderr)return strip_rtf_fallback(raw), "rtf-regex"diff --git a/scripts/extractor/parsers/text.py b/scripts/extractor/parsers/text.pyindex 56a3b6d..cfe74a1 100644--- a/scripts/extractor/parsers/text.py+++ b/scripts/extractor/parsers/text.py@@ -1,5 +1,5 @@from __future__ import annotations-+import sysfrom pathlib import Path@@ -9,6 +9,7 @@ def read_text_file(path: str) -> str | None:return Path(path).read_text(encoding=encoding)except UnicodeDecodeError:continue- except Exception:+ except Exception as e:+ print(f" [warn] read_text_file failed: {type(e).__name__}: {e}", file=sys.stderr)return Nonereturn Nonediff --git a/tests/test_extractor.py b/tests/test_extractor.pyindex ee0a379..e5ff7b8 100644--- a/tests/test_extractor.py+++ b/tests/test_extractor.py@@ -883,3 +883,33 @@ class TestDependencyCheck:# the PDF (text-heavy) group line should be followed by a "ready" statuspdf_block = out.split("PDF (text-heavy)", 1)[1].split("PDF (technical", 1)[0]assert "ready" in pdf_block+++# ---------------------------------------------------------------------------+# Parser exception logging+# ---------------------------------------------------------------------------++class TestParserExceptionLogging:+ """Verify unexpected parser exceptions surface on stderr, chain returns None."""++ def test_pypdf2_warns_on_unexpected_error_and_returns_none(self, tmp_path, capsys):+ """Monkeypatch pypdf2 import to raise; confirm None + stderr warning."""+ from extractor.parsers.pdf import extract_with_pypdf2++ broken = tmp_path / "broken.pdf"+ broken.write_bytes(b"%PDF-1.4 fake")++ real_import = __import__++ def fake_import(name, *args, **kwargs):+ if name == "PyPDF2":+ raise RuntimeError("simulated failure")+ return real_import(name, *args, **kwargs)++ with mock.patch("builtins.__import__", side_effect=fake_import):+ result = extract_with_pypdf2(str(broken))++ assert result is None+ captured = capsys.readouterr()+ assert "[warn]" in captured.err+ assert "failed:" in captured.err\ No newline at end of file