Skip to main content

ephemeris_report/
error.rs

1use std::error::Error;
2use std::io;
3use std::path::PathBuf;
4use std::str::Utf8Error;
5
6use facet_json::DeserializeError;
7
8/// Why a report could not be written or read.
9#[derive(Debug, thiserror::Error)]
10pub enum ReportError {
11    /// A file or directory could not be accessed.
12    #[error("cannot access {}", path.display())]
13    Io {
14        /// The path that failed.
15        path: PathBuf,
16        /// The underlying error.
17        #[source]
18        source: io::Error,
19    },
20    /// The report could not be encoded as JSON.
21    #[error("cannot encode the report")]
22    Encode {
23        /// The encoder error.
24        #[source]
25        source: Box<dyn Error + Send + Sync>,
26    },
27    /// The report file is not a valid report.
28    #[error("cannot decode {}", path.display())]
29    Decode {
30        /// The report file.
31        path: PathBuf,
32        /// The decoder error.
33        #[source]
34        source: Box<DeserializeError>,
35    },
36}
37
38impl ReportError {
39    pub(crate) fn io(path: impl Into<PathBuf>, source: io::Error) -> Self {
40        Self::Io {
41            path: path.into(),
42            source,
43        }
44    }
45}
46
47/// Why native tool output could not be converted.
48#[derive(Debug, thiserror::Error)]
49pub enum ConvertError {
50    /// The output is not valid UTF-8.
51    #[error("the output is not valid UTF-8")]
52    Utf8(#[from] Utf8Error),
53    /// The output is not well-formed XML.
54    #[error("the output is not valid XML")]
55    Xml {
56        /// The parser error.
57        #[source]
58        source: roxmltree::Error,
59    },
60    /// A line of the output is not a valid JSON message.
61    #[error("line {line} is not a valid message")]
62    Json {
63        /// The one-based line number.
64        line: usize,
65        /// The decoder error.
66        #[source]
67        source: Box<DeserializeError>,
68    },
69}