DEV Community

Pocket PDF
Pocket PDF

Posted on AI-assisted

Designing Error Handling for Browser-Based File Converters

A conversion button is easy to build.

A conversion workflow that behaves sensibly when something goes wrong takes considerably more thought.

File processing has plenty of opportunities to fail. A document might be damaged, a format might be unsupported, a browser might run out of memory or a processing library might reject a particular feature.

The interface needs to handle these situations without leaving the user confused.

  1. Separate the processing stages

One mistake is treating the entire workflow as a single operation.

A better approach is to think in stages:

File selection.

Initial validation.

Reading the file.

Processing.

Output generation.

Download preparation.

Each stage has different failure possibilities.

For example, an empty file can be rejected before processing starts.

A password-protected PDF might only fail when the PDF library attempts to load it.

A download issue can happen after the conversion itself has succeeded.

Keeping these stages separate makes errors easier to understand and debug.

  1. Use a predictable state model

For a simple application, a small state model is often enough.

const states = {
IDLE: "idle",
VALIDATING: "validating",
PROCESSING: "processing",
COMPLETE: "complete",
ERROR: "error"
};

The interface can use these states to determine which controls should be available.

For example:

Disable the convert button while processing.

Allow file replacement after an error.

Show a download action only after output generation succeeds.

Clear outdated errors when a new file is selected.

This prevents awkward situations where the interface claims that processing is complete while the output is still being generated.

  1. Catch errors at the right level

A broad try/catch is useful, but it shouldn't become an excuse to hide every failure behind the same message.

Consider:

async function processFile(file) {
try {
if (!file || file.size === 0) {
throw new Error("EMPTY_FILE");
}

const result = await convertFile(file);

return {
  success: true,
  result
};
Enter fullscreen mode Exit fullscreen mode

} catch (error) {
console.error("File processing failed:", error);

return {
  success: false,
  error: getFriendlyMessage(error)
};
Enter fullscreen mode Exit fullscreen mode

}
}

The application can log technical details for debugging while presenting a clearer explanation to the user.

Be careful about error classification.

A generic JavaScript exception doesn't necessarily mean a file is corrupted. It could be an implementation bug or an unexpected browser condition.

When the cause is unknown, say that processing failed rather than inventing a reason.

  1. Don't forget cleanup

Browser applications often create temporary resources.

Examples include:

Object URLs created with URL.createObjectURL().

Event listeners.

Web Workers.

Temporary references to large buffers.

Audio or video processing resources.

Object URLs should be released when no longer needed:

const url = URL.createObjectURL(blob);

// Use the URL for the download.

URL.revokeObjectURL(url);

For longer-running workflows, cleanup becomes particularly important.

A user who processes several large files shouldn't accumulate unnecessary resources every time they click the button.

  1. Test failure paths deliberately

Don't only test whether the final file downloads.

Try selecting an empty file, a wrong format, a damaged document and an unusually large input.

Cancel operations where cancellation is supported.

Try processing another file after a previous failure.

Refresh during an operation and see whether the application returns to a sensible state.

These tests often reveal interface problems that successful conversions won't expose.

Final thought

Error handling isn't a secondary feature.

For file utilities, it's part of the actual product.

I've been applying these ideas while developing Pocket PDF, a browser-based collection of file utilities available at https://www.pocketpdf.site.

There is always another unusual input to discover. Apparently, people have managed to create more ways to break a file format than anyone originally planned for.

What's the most useful error message you've seen in a developer tool, and what made it useful?

Top comments (3)

Collapse
 
pocketpdf profile image
Pocket PDF •

The error-handling point is underrated. Showing a generic failure message makes debugging harder for developers and gives users nothing useful to do next. Separating validation, processing and output errors makes a noticeable difference. I've been applying that approach at pocketpdf.site.

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

Thanks — and the fact you're already applying the staged approach at pocketpdf.site is the right instinct. The one addition I'd make from the extraction side: the silent case is the one staging alone doesn't catch. A file can pass validation, processing and output cleanly and still be empty where it matters — a scanned page that converts to zero characters looks like a success at every stage.

If it's useful, I can share a small fixture from my benchmark set — a 3-page scanned PDF plus the per-page char counts my extractor reports — so you can see what the "unindexed page" signal looks like in practice. Happy to compare notes on where you'd surface that warning in the UI.

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

The failure mode this misses is the one that cost me the most time: the conversion that succeeds and looks complete while silently dropping content. A scanned PDF converted to markdown with exit code 0 and an empty file is worse than any error dialog — the user has no signal at all, and anything downstream (a RAG index, a search) treats the document as indexed when it holds nothing.

So I'd split your stage model one level further: every stage needs a loud failure and a silent-failure check. For extraction that means a per-page completeness signal — character count per page after conversion, pages under ~50 chars routed to OCR or flagged "unindexed" instead of passing through as empty. The test that catches it is exactly the one you list last: try a scanned page, a 90° rotated page, a table-heavy page, and assert on the output (does page 3 have text? does the table survive as markdown?) rather than on the exit code.

On your closing question — the most useful error message I've seen in a dev tool names what was lost and where: "page 3 produced 0 characters — likely a scanned image, OCR required" beats "conversion failed" because it tells the user the next action. That's the rule I built into pdf-slice (per-page text-layer vs OCR routing, tables kept as markdown), after benchmarking MarkItDown 0.1.9 returning an empty file for a scanned PDF: github.com/kamalmosadegh/pdf-slice — and the benchmark write-up: dev.to/kamalmosadegh/markitdown-re...