← All articles
IoT · Quality

Fail-Fast protocol adapters: why your gateway must refuse bad data

The most dangerous code in an industrial gateway is the code that “helpfully” fixes bad data. It turns corruption into a lie that travels downstream.

Fail-Fast as a contract

Every EdgeStream-GW protocol adapter implements one promise: parse(payload) → Frame, or it raises CorruptedPayloadError. Nothing else. There is no partial frame, no best-effort repair, no silent default.

The error carries a structured reason_code — TRUNCATED, BAD_CRC, WRONG_FC, INCOMPLETE_ARRAY, OUT_OF_RANGE, BAD_UNIT_ID, WRONG_PROTOCOL — so downstream code can react programmatically instead of string-matching an error message.

Why not reuse a parser?

Open-source Modbus parsers are usually lenient: they let incomplete arrays through, or they “fix” an off-by-one. That is reasonable for a lab tool and unacceptable for a gateway that must guarantee CMMI Level 3 quality. So the adapters are built in-house (a deliberate build decision), while the compliant protocol stacks underneath remain reused.

Purity makes it testable

An adapter never reads the wall clock and never touches I/O. The timestamp is injected via RawPayload.collected_at. The result: parse is a pure function — same bytes in, same Frame out — which is exactly what makes it deterministically testable and replayable.

Proving the invariant with a machine

Hand-written tests cannot prove “nothing unexpected ever escapes.” So we use property-based testing: hundreds of arbitrary byte strings are fed to parse, and the invariant is that the result is either a valid Frame or a CorruptedPayloadError — never any other exception, never a crash.

Fail-Fast is a habit, not a feature: refuse loudly at the boundary, so everything downstream can trust its input.