Skip to content

Commit 1fdea5f

Browse files
authored
fix: wrap BadZipFile and FileNotFoundError in library exceptions (#5)
unpack_document now raises InvalidDocumentError for corrupt .docx files and DocumentNotFoundError for missing files, instead of leaking zipfile.BadZipFile and FileNotFoundError. Closes #4
1 parent 96b20b6 commit 1fdea5f

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎docx_editor/ooxml/unpack.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import defusedxml.minidom
88

9+
from docx_editor.exceptions import DocumentNotFoundError, InvalidDocumentError
10+
911

1012
def unpack_document(input_file: str | Path, output_dir: str | Path) -> str:
1113
"""Unpack a .docx file to a directory with pretty-printed XML.
@@ -18,18 +20,21 @@ def unpack_document(input_file: str | Path, output_dir: str | Path) -> str:
1820
str: Suggested RSID for edit session (8-character hex string)
1921
2022
Raises:
21-
FileNotFoundError: If the input file doesn't exist
22-
zipfile.BadZipFile: If the file is not a valid zip/docx
23+
DocumentNotFoundError: If the input file doesn't exist
24+
InvalidDocumentError: If the file is not a valid zip/docx
2325
"""
2426
input_path = Path(input_file)
2527
output_path = Path(output_dir)
2628

2729
if not input_path.exists():
28-
raise FileNotFoundError(f"Document not found: {input_file}")
30+
raise DocumentNotFoundError(f"Document not found: {input_file}")
2931

3032
# Extract and format
3133
output_path.mkdir(parents=True, exist_ok=True)
32-
zipfile.ZipFile(input_path).extractall(output_path)
34+
try:
35+
zipfile.ZipFile(input_path).extractall(output_path)
36+
except zipfile.BadZipFile as e:
37+
raise InvalidDocumentError(f"Not a valid .docx file: {input_file}") from e
3338

3439
# Pretty print all XML files
3540
xml_files = list(output_path.rglob("*.xml")) + list(output_path.rglob("*.rels"))

‎tests/test_ooxml.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
from docx_editor.exceptions import DocumentNotFoundError, InvalidDocumentError
56
from docx_editor.ooxml.pack import pack_document
67
from docx_editor.ooxml.unpack import unpack_document
78

@@ -11,17 +12,15 @@ class TestUnpack:
1112

1213
def test_unpack_nonexistent_file(self, temp_dir):
1314
"""Test error when unpacking nonexistent file."""
14-
with pytest.raises(FileNotFoundError, match="Document not found"):
15+
with pytest.raises(DocumentNotFoundError, match="Document not found"):
1516
unpack_document(temp_dir / "nonexistent.docx", temp_dir / "output")
1617

1718
def test_unpack_invalid_zip(self, temp_dir):
1819
"""Test error when unpacking invalid zip file."""
19-
import zipfile
20-
2120
invalid_file = temp_dir / "invalid.docx"
2221
invalid_file.write_text("not a zip file")
2322

24-
with pytest.raises(zipfile.BadZipFile):
23+
with pytest.raises(InvalidDocumentError, match="Not a valid .docx file"):
2524
unpack_document(invalid_file, temp_dir / "output")
2625

2726
def test_unpack_returns_rsid(self, simple_docx, temp_dir):

0 commit comments

Comments
 (0)