← Weekly essays

The catch block is cheap. It is the bookkeeping that costs you.

#data#web#solo-founder#product

A row of quiet cards with one empty dashed slot, and below it a tray holding the tilted bad card next to an amber record card with ruled lines

A feed parser on Statpro threw on the first malformed item this week and took every good item on the page down with it. One bad row erased ten good ones. I fixed it with a per-item try/catch and felt clever for about five minutes. The catch was three lines, and the rest of the afternoon went to the part that decides whether those three lines are an improvement or a quieter way to lose data.

That part is bookkeeping. Every quarantined item now gets a structured failure record, and without it, swallowing the error is worse than crashing. I'd be trading a loud failure everyone notices for a silent one nobody can debug. Most parsers default to all-or-nothing, and I think that default is rational, so I owe an explanation for fighting it.

Statpro is a sports stats site I build. The feed in question is a stream of short player news items, each about one player, that the site shows on its news pages. A parser reads each page of the feed and turns every item into a news card.

All-or-nothing is the rational default

When a parser throws, the page breaks and someone notices. When it swallows errors, the page looks fine and the bad data disappears into a gap I won't find for weeks. The all-or-nothing instinct exists because a crash is the only failure that announces itself. I don't need instrumentation to know I crashed.

Partial parsing is a different contract. It says that some items are allowed to fail, that I will catch them, that I will know which ones, and that I will have enough information to fix them without reproducing the input. Every one of those clauses is a commitment. Miss one and I've built a silent data-loss machine that is strictly worse than the crash it replaced.

That is why the try/catch is three lines and the bookkeeping is the rest of the function. The catch answers what to do with this error right now. The bookkeeping answers how I'll debug it at 11pm on a Tuesday, looking at input I have never seen before, and that second question is the one that matters.

What a quarantine record has to hold

A logged error message isn't enough for that. "Unexpected token at position 47" tells me the parser choked, which I already knew. To debug a quarantined item later, I need four things:

  • Which item failed, as an index on the page, so I can find it in the raw input.
  • Which field triggered it, meaning the key that was being read when the parser threw.
  • The error type, because a parsing failure, a missing field and a type mismatch each need a different fix.
  • Enough of the raw item to reproduce the failure without fetching it again, because the source doesn't stay available forever.

This is the shape of the loop once that record exists, simplified from the real parser:

for (const [index, raw] of rawItems.entries()) {
  try {
    items.push(parseItem(raw, { ...context, index }));
  } catch (error) {
    const failure = classifyFailure(error, raw);
    if (!failure) throw error; // unknown errors still throw
    failures.push(failure);
  }
}

The line that matters most is the one that re-throws. Unknown errors are not quarantined, so if the parser hits something it wasn't built to classify, it crashes the page. A crash I can debug beats a swallowed error I can't. Quarantine is reserved for failures I have already named, and it stays closed to the ones I haven't met yet.

ALL-OR-NOTHING 11 items, 1 malformed Parser throws Good items lost too 0 of 11 shown QUARANTINE 11 items, 1 malformed Per-item try/catch 10 good items parsed Failure record for the bad item 10 of 11 shown index, field, error type, raw item unknown errors still throw
Quarantine only beats crashing once the failure record makes the bad item debuggable without reproducing the input.

An empty page still throws

If every item on a page fails, the parser still throws. That felt wrong at first, since I had just built a quarantine and wasn't using it. But a page where zero items survive isn't a partial success. It's a signal that the source changed its format, or the feed is down, or something structural broke. Quarantining everything would bury that signal under a pile of individual failures that all share one root cause.

The rule I landed on is that partial parsing is for pages where most items succeed. A page where nothing succeeds is a different kind of failure and deserves a different kind of alarm.

The same rule for news cards

The same shape showed up in a second fix this week. News cards, the short items from that feed, now show player headshots and team logos, but only after a verification step confirms the player exists in the database. If verification fails, the card renders as plain text, with no broken image and no link to a 404.

It's the same pattern at a different scale. Quarantine means parse what I can and record what I can't. Verifying before decorating means link what I can prove and render plain text for the rest. Both fail soft only when the failure has a name and an entry in a record. An unverified player isn't an error to hide. It's a decoration to skip, with a note of why.

When I'd keep the crash

I wouldn't reach for partial parsing on a feed I controlled end to end. When I write both the producer and the consumer, a malformed item is a bug to fix at the source, and papering over it in the parser hides it. Crashing is the correct pressure there, because it pushes the fix upstream where it belongs.

Quarantine earns its place on feeds I don't control, which for a solo founder is most of them. I can't stop a source from shipping a malformed box score at 2am. I can decide whether one bad row takes the page down. The answer should be no, and the bookkeeping is the only thing that makes that answer safe.

The catch block was three lines. The structured failure record, the re-throw for unknown errors and the empty-page guard took the afternoon, and they are what make the three lines worth writing.