Founder Notes
I Asked For It After It Was Gone
A batch of items came in, each one written separately, and if any single item was rejected by a business rule the whole request came back as a server error instead of a tidy report saying which item had been refused and why. The rejection logic was sitting right there and it looked fine. I assumed the trouble was in how the per-item message got assembled, some stale value being read where a fresh one was meant.
That was wrong, and the way it was wrong has stayed with me.
When a write is abandoned partway through, the store puts the records it had been holding back into a state where the next question about them has to go and ask again. That is the store doing its job. But it means the record my error handler was about to use was no longer something I had. It was something I would have to go and fetch.
And the first act of the handler was to read one field off that record, to put the item's own name into the message explaining the refusal. On the run where everything succeeded, the record was still in hand and the question answered itself where it stood. On the run where something had been refused, the same line was a question that had to travel out to the store, asked from inside a failure, where that journey is not available. So it raised. The handler that existed to describe a small, expected, thoroughly understood refusal was the thing turning it into a server error.
Recovery code is the only code that runs in conditions the failure made
Here is the general shape, and it is the part I had never put into words.
Almost everything I write runs in ordinary conditions. Recovery code does not. It is the only code in a program whose entire execution happens after something has gone wrong, which means it runs in a world the failure has already rearranged. What it can reach is not what the surrounding function could reach a moment earlier. By the time it begins, some of what it wants to talk about has been handed back.
So a handler carries a precondition that nothing else around it carries: whatever it needs in order to do its job has to be taken before the thing that can fail, not after. Not because asking later is slower. Because asking later is asking in a different world, and the answer may not be available there at all.
The repair was to lift the value out above the operation and hold it in a plain variable. Three words moved. The reason was not efficiency. The reason was that the value had to be captured while there was still something there to capture.
Why this kind of bug lasts
What makes it durable is that the failing line also appears on the run that works, and there it is unremarkable. Read the function and you see one expression used in two places, doing the obvious thing in both. Nothing marks the second one as different. The difference is not in the line. It is in everything around the line, and everything around the line is exactly what the failure changed.
I had also been reading a handler as a description of the problem rather than as code with requirements of its own. It is a sentence about what went wrong. It is also a program, with dependencies, running at the worst available moment under the narrowest available conditions.
What I do now
When I write a handler, I work out what it is going to need, and I go and get those things before the call that can fail rather than inside the consequences of it. Names, identifiers, anything I mean to put in front of a person later, all taken early and held plainly, so the handler works from what it is already holding instead of asking for something back.
Error handling runs in a world the error has already changed. Whatever it needs, it has to be holding before the failure, because afterwards it is no longer in a position to ask.