Skip to content

Narrow except Exception in extractor.py:96 to specific PDF error #30

Description

@AshayK003

Title: Narrow except Exception in extractor.py:96 to specific PDF error

Body:

Problem

In diffiq/extractor.py, line 96 catches Exception broadly:

try:
    reader = pypdf.PdfReader(io.BytesIO(raw_bytes))
    pages: list[str] = []
    for page in reader.pages:
        text = page.extract_text()
        if text:
            pages.append(text)
except Exception as e:
    logger.warning("pypdf extraction failed for %s: %s", pdf_url, e)
    return ExtractionResult(text=None, error=f"Corrupted PDF: {e}")

This catch is too broad — it would silently swallow KeyboardInterrupt, MemoryError, or any unexpected bug in the loop logic. The only expected failure here is a corrupted/invalid PDF, which pypdf raises as PdfReadError.

Solution

Replace except Exception with except pypdf.errors.PdfReadError:

except pypdf.errors.PdfReadError as e:
    logger.warning("pypdf extraction failed for %s: %s", pdf_url, e)
    return ExtractionResult(text=None, error=f"Corrupted PDF: {e}")

Acceptance Criteria

  • Exception is no longer caught — only pypdf.errors.PdfReadError
  • pytest tests/ -v passes
  • Existing PDF corruption handling still works (tested via test data)

Difficulty

Easy — 1 exception type change, 1 file

Size

XS — 1 line

Files Affected

diffiq/extractor.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions