Returning Typed Records and Diagnostics from a Reader

A reader can expose parsed values together with source locations or reasons for rejected records, according to its error policy.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace an optional row
  2. Decide whether to continue
  3. Keep good rows and errors in a documented result model
  4. Define whether accepted output is provisional

Trace an optional row

While reading a catalogue, a normal row can return ID and quantity as typed fields. A malformed quantity can produce a row-specific diagnostic. Typed conversions separate input spelling from stored meaning.

Decide whether to continue

An interactive tool may gather several diagnostics before asking for repair. A batch update may require all rows to be valid before changing storage. Use structured decisions to convey completion status to the caller.[1]

Keep good rows and errors in a documented result model

A file importer can return a typed accepted record for each valid row and a diagnostic carrying row number and reason for a failed row. This allows a caller to collect several independent failures without inventing placeholder values. See error results and exceptions.

Row 1: "A,4" → accepted {label:A, count:4}
Row 2: "B,many" → rejected {row:2, field:count, reason:invalid integer}
Row 3: "C,0" → accepted with count zero

Define whether accepted output is provisional

Some callers need all-or-nothing publication even when parsing can accumulate diagnostics. Keep that transaction decision above the parser. Test two errors and two valid records in one input to verify retained locations and order.

References

  1. Python Software Foundation, io: Core Tools for Working with Streams ↩