Distinguishing File Open, Read, and Decode Errors

Opening a path, reading data, and interpreting what was read can fail for different reasons.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Follow the path first
  2. Choose distinct reported outcomes
  3. Keep open, read, decode and validation errors distinguishable
  4. Attach relevant context where it becomes known

Follow the path first

An absent source fails when opening or locating it. A readable source may later fail parsing because the format is wrong. Absolute and relative paths help investigate which file was actually requested.

Choose distinct reported outcomes

For a reader used by another command, include enough structured context for it to distinguish no file, incomplete file, and invalid data. The result or exception should implement a documented function contract usable by both tests and callers.[1]

Keep open, read, decode and validation errors distinguishable

A missing filename fails when opening. An existing file can fail during a later read. Invalid UTF-8 fails while decoding, and an otherwise valid text record can fail schema checks. Reporting all four as “file missing” misdirects the person debugging. See text decoding error policies.

Example errors by phase
Open → path absent
Read → I/O failure after header
Decode → invalid sequence at byte offset
Validate → unsupported field value

Attach relevant context where it becomes known

Include the intended path, read location or record key as appropriate. Preserve the underlying failure for diagnosis when translating it into an application error. Test one synthetic failure per stage.

References

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