See Ash Code logo See Ash Code

An invalid PDF should produce a useful failure

Ashlynn Antrobus
• • 3 min read

If somebody passes my PDF converter a file that isn’t a PDF, what should they get back?

A dragon examines a crumpled page beside a scanner displaying a warning.

An exception is a useful answer. An empty result that looks like a successful conversion is a much less useful one.

On December 16, I changed ebook2text’s PDF reader to handle PDFSyntaxError. Then I had to change what happened after handling it.

Logging the error wasn’t enough

The reader uses a generator to return pages from pdfminer. In the first change, I added the parser’s syntax exception to the errors it caught:

try:
    yield from extract_pages(file_path, maxpages=25)
except (PDFSyntaxError, OSError) as e:
    logger.error(f"Error reading PDF file: {e}")

The log gives me a message about the failed read. But after writing that message, the generator reaches its end. A caller consuming the pages doesn’t receive the exception that caused it to stop.

That can make a failed conversion look like a conversion with nothing to return. The person using the library might not see the log, and their code needs something it can act on anyway.

The follow-up change adds one line:

except (PDFSyntaxError, OSError) as e:
    logger.error(f"Error reading PDF file: {e}")
    raise

The bare raise re-raises the exception being handled. I keep the diagnostic message, and the caller gets the failure instead of having to guess from missing output.

The test has to read the generator

For the regression test, I didn’t need a large damaged document. I wrote a few bytes to a temporary file with a PDF extension:

file_content = b"Not a real PDF"
file_name = "test.pdf"
file_path = tmp_path / file_name
file_path.write_bytes(file_content)
pdf_converter = PDFConverter(file_path, metadata, pdf_text_extractor)
with pytest.raises(PDFSyntaxError):
    list(pdf_converter.pages)

The extension makes it look like the expected input. The bytes make it invalid.

The placement of the assertion matters. Creating a converter isn’t the same as consuming its pages. The constructor stores a generator; the parser runs as that generator is advanced. The list call forces the test to ask for the pages and encounter the parsing error.

If I wrapped only the constructor in pytest.raises, I would be checking the wrong operation. A lazy interface makes the point of failure part of the interface too.

What the caller can rely on

The revised reader logs and propagates the syntax or operating-system error it catches. A caller can catch that error around the operation that consumes the pages, show a useful message, or decide whether to try another file.

That doesn’t make partial output a completed conversion. A generator can have yielded some work before a later failure. Code that writes results as it reads still needs to decide what happens to that partial result.

The log records what went wrong. Raising the exception lets the caller decide what to do with any output it has already received. The generator’s input came from the constructor refactor.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.