Skip to content

Fix: wrap exceptions in library error types - #5

Merged
pablospe merged 1 commit into
mainfrom
zipfile-invalid-error
Jan 31, 2026
Merged

pablospe merged 1 commit into
mainfrom
zipfile-invalid-error

Conversation

@pablospe

@pablospe pablospe commented Jan 31, 2026 •

Copy link
Copy Markdown
Owner

Wrap BadZipFile and FileNotFoundError in InvalidDocumentError and DocumentNotFoundError respectively, so callers only need to catch DocxEditError subclasses.

Closes #4

Summary by CodeRabbit

  • Bug Fixes

    • Improved error messages when opening document files with clearer distinction between missing files and invalid documents.
  • Tests

    • Updated test cases to verify enhanced error handling and messaging.

✏️ Tip: You can customize this high-level summary in your review settings.

unpack_document now raises InvalidDocumentError for corrupt .docx files
and DocumentNotFoundError for missing files, instead of leaking
zipfile.BadZipFile and FileNotFoundError.

Closes #4
@coderabbitai

coderabbitai Bot commented Jan 31, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Custom exception handling is introduced to docx_editor/ooxml/unpack.py to wrap standard exceptions with domain-specific ones: DocumentNotFoundError for missing files and InvalidDocumentError for invalid .docx files. Tests are updated to expect the new exception types.

Changes

Cohort / File(s) Summary
Exception handling implementation
docx_editor/ooxml/unpack.py
Replaces FileNotFoundError with DocumentNotFoundError for missing input paths; wraps zipfile.BadZipFile in a try/except block to raise InvalidDocumentError with message "Not a valid .docx file" for corrupted documents.
Test updates
tests/test_ooxml.py
Updates error test expectations to assert DocumentNotFoundError for missing files and InvalidDocumentError for invalid .docx files; removes inline zipfile import and BadZipFile assertion.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Exceptions wrapped with care so fine,
No more zipfile errors to cross the line,
Now InvalidDocumentError takes its place,
Corrupt .docx files handled with grace!

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch zipfile-invalid-error

Comment @coderabbitai help to get the list of available commands and usage tips.

@pablospe
pablospe merged commit 1fdea5f into main Jan 31, 2026
7 of 8 checks passed
@codecov

codecov Bot commented Jan 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.5%. Comparing base (96b20b6) to head (a98bf47).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main      #5   +/-   ##
=====================================
  Coverage   93.5%   93.5%           
=====================================
  Files         10      10           
  Lines       1863    1867    +4     
  Branches     433     433           
=====================================
+ Hits        1742    1746    +4     
  Misses        33      33           
  Partials      88      88           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidDocumentError not raised for corrupt .docx files

1 participant