use std::path::PathBuf;
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CheckpointError {
#[error(
"failed to serialize or deserialize checkpoint state: {0}. Fix: ensure the state type is serializable and the file contains valid JSON."
)]
Serialize(#[from] serde_json::Error),
#[error("checkpoint I/O failed at `{path}`: {source}. Fix: ensure the parent directory exists and is writable.")]
Io {
path: PathBuf,
source: std::io::Error,
},
#[error("disk full at `{path}`: {source}. Fix: free up disk space before saving checkpoint.")]
DiskFull {
path: PathBuf,
source: std::io::Error,
},
#[error("path traversal detected in `{0}`. Fix: ensure path does not contain `..` parent directory traversal.")]
PathTraversal(PathBuf),
#[error("checkpoint at `{0}` is empty or truncated (0 bytes). Fix: delete it to restart from scratch, or restore it from a backup - an empty checkpoint is never valid.")]
CorruptCheckpoint(PathBuf),
}
impl CheckpointError {
#[must_use]
pub const fn is_disk_full(&self) -> bool {
matches!(self, Self::DiskFull { .. })
}
#[must_use]
pub fn from_io_error(path: PathBuf, source: std::io::Error) -> Self {
use std::io::ErrorKind;
let mut disk_full = source.kind() == ErrorKind::StorageFull;
#[cfg(unix)]
if !disk_full {
disk_full = source.raw_os_error() == Some(libc::ENOSPC);
}
if disk_full {
Self::DiskFull { path, source }
} else {
Self::Io { path, source }
}
}
}