Skip to main content

oino_session/
error.rs

1//! Error type of the session crate.
2
3use std::path::PathBuf;
4
5use crate::entry::EntryId;
6
7/// Errors raised by parsing, migration and [`crate::SessionManager`] operations.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum SessionError {
11    /// File-system failure.
12    #[error("I/O error on {path}: {source}")]
13    Io {
14        /// Affected path.
15        path: PathBuf,
16        /// Underlying error.
17        source: std::io::Error,
18    },
19    /// A line could not be (de)serialised.
20    #[error("invalid JSON in {path} line {line}: {source}")]
21    Json {
22        /// Affected path (empty for in-memory data).
23        path: PathBuf,
24        /// 1-based line number (0 when not line based).
25        line: usize,
26        /// Underlying error.
27        source: serde_json::Error,
28    },
29    /// Session id failed validation (message text is fixed, verbatim).
30    #[error(
31        "Session id must be non-empty, contain only alphanumeric characters, '-', '_', and '.', and start and end with an alphanumeric character"
32    )]
33    InvalidSessionId(String),
34    /// Entry id not present.
35    #[error("Entry {0} not found")]
36    UnknownEntry(EntryId),
37    /// Session id prefix matched several files.
38    #[error("session id is ambiguous: {} matches", .0.len())]
39    AmbiguousId(Vec<PathBuf>),
40    /// File written by a newer session format.
41    #[error("unsupported session version {0}")]
42    UnsupportedVersion(u32),
43    /// The file has content but no valid header line.
44    #[error("Session file is not a valid Oino session: {}", .0.display())]
45    MissingHeader(PathBuf),
46    /// The header has no cwd and no override was supplied.
47    #[error("session header has no cwd: {}", .0.display())]
48    MissingCwd(PathBuf),
49    /// The append is not allowed (reason given).
50    #[error("invalid append: {0}")]
51    InvalidAppend(&'static str),
52    /// Required file does not exist.
53    #[error("not found: {}", .0.display())]
54    NotFound(PathBuf),
55    /// Listing was cancelled through the cancel flag.
56    #[error("operation cancelled")]
57    Cancelled,
58}
59
60/// Failure of acquiring the single-writer session lease (D-LOCK, spec core.md ยง2.1).
61#[cfg(feature = "manager")]
62#[derive(Debug, thiserror::Error)]
63pub enum SessionLockError {
64    /// Another writer holds the lease (this process or another runner).
65    #[error("session is locked by {}", .0.describe())]
66    HeldBy(crate::manager::LeaseInfo),
67    /// Lock file failure.
68    #[error("I/O error on {path}: {source}")]
69    Io {
70        /// Affected path.
71        path: PathBuf,
72        /// Underlying error.
73        source: std::io::Error,
74    },
75}
76
77#[cfg(feature = "manager")]
78impl SessionError {
79    /// Wraps an I/O error with its path.
80    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
81        Self::Io {
82            path: path.into(),
83            source,
84        }
85    }
86}