Skip to main content

agent_sessions/
error.rs

1use serde::Serialize;
2use std::{fmt, io, path::PathBuf};
3
4/// The failed discovery operation. `InspectFile` identifies a candidate already
5/// observed as a regular JSONL file, so consumers can apply their metadata policy.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum DiscoverOperation {
9    DirectoryMetadata,
10    ReadDirectory,
11    ReadEntry,
12    FileType,
13    InspectFile,
14}
15
16#[derive(Debug)]
17pub struct DiscoverError {
18    pub agent: crate::Agent,
19    pub operation: DiscoverOperation,
20    pub path: PathBuf,
21    pub source: io::Error,
22}
23
24#[derive(Debug)]
25#[non_exhaustive]
26pub enum ReadError {
27    Io(io::Error),
28    TooLarge { limit: u64 },
29    InvalidOptions(&'static str),
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
33#[non_exhaustive]
34pub enum LineErrorKind {
35    InvalidJson,
36    InvalidUtf8,
37    TooLong,
38    MissingField(&'static str),
39    InvalidField(String),
40    CounterRegression,
41    LostUsageBaseline,
42}
43
44#[derive(Debug)]
45#[non_exhaustive]
46pub enum StreamError {
47    Line {
48        line_no: u64,
49        byte_start: u64,
50        kind: LineErrorKind,
51    },
52    Io(io::Error),
53    TooLarge {
54        limit: u64,
55    },
56    SnapshotTruncated {
57        expected: u64,
58        actual: u64,
59    },
60}
61
62impl fmt::Display for ReadError {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        match self {
65            Self::Io(_) => write!(f, "cannot open session file"),
66            Self::TooLarge { limit } => write!(f, "session exceeds {limit} byte limit"),
67            Self::InvalidOptions(msg) => write!(f, "invalid read options: {msg}"),
68        }
69    }
70}
71impl std::error::Error for ReadError {
72    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
73        match self {
74            Self::Io(e) => Some(e),
75            _ => None,
76        }
77    }
78}
79impl fmt::Display for StreamError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            Self::Line { line_no, kind, .. } => write!(f, "session line {line_no}: {kind:?}"),
83            Self::Io(_) => write!(f, "session read failed"),
84            Self::TooLarge { limit } => write!(f, "session exceeds {limit} byte limit"),
85            Self::SnapshotTruncated { expected, actual } => {
86                write!(f, "snapshot requires {expected} bytes; read {actual}")
87            }
88        }
89    }
90}
91impl std::error::Error for StreamError {
92    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
93        match self {
94            Self::Io(e) => Some(e),
95            _ => None,
96        }
97    }
98}