OSError and parser exceptions into BioptimusError with a stable ErrorCode, structured context; remediation text lives centrally with the error code.
The two context managers are applied at the exact boundary call (open, yaml.safe_load, Path.mkdir) so that domain logic stays outside the try and the translation policy lives in one place rather than being copy-pasted into every reader and writer. The approach mirrors how mature libraries centralize exception translation, for example httpx’s map_httpcore_exceptions context manager and PyTorch’s _check_seekable boundary validator.
To keep this module dependency-light (standard library only), parser exception types that are not built in (for example yaml.YAMLError) are not imported here; callers pass them via the malformed argument of fs_read.
fs_read
BioptimusValueError with code ErrorCode.INVALID_CONFIG, attaching the path and errno to the context and an errno-specific remediation hint when available. Already-typed BioptimusError instances raised inside the block propagate unchanged so that domain errors keep their original code.
The filesystem path being read, used for structured context.
A short noun phrase naming the artifact, for example
"cohort CSV manifest". It is interpolated into the message.Additional, non-built-in exception types (for example
yaml.YAMLError or csv.Error) that signal malformed content and should be reported as ErrorCode.INVALID_CONFIG. Built-in UnicodeDecodeError is always treated as malformed.with block into a typed BioptimusValueError with code
ErrorCode.INVALID_CONFIG.
fs_write
OSError failures are converted into a BioptimusRuntimeError: code ErrorCode.DISK_FULL when the volume is out of space or over quota, or ErrorCode.OUTPUT_WRITE_FAILED otherwise, attaching the path to the context. Already-typed BioptimusError instances raised inside the block propagate unchanged.
The destination path being written, used for structured context.
A short noun phrase naming the artifact, for example
"cohort manifest". It is interpolated into the message.with block into a typed BioptimusRuntimeError with code
ErrorCode.DISK_FULL or ErrorCode.OUTPUT_WRITE_FAILED.
