Skip to main content

libmseed/
error.rs

1//! Error type for the crate.
2
3/// Result alias used throughout the crate.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Errors returned by library functions.
7///
8/// Variants say what kind of problem occurred; the message says where and why, usually
9/// starting with the source identifier of the record concerned.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    /// The data is not miniSEED.
14    #[error("not miniSEED: {0}")]
15    NotSeed(String),
16    /// A record length is outside the supported range.
17    #[error("record length out of range: {0}")]
18    OutOfRange(String),
19    /// The input ended before a complete record was available.
20    #[error("incomplete record, {needed} more bytes needed")]
21    Incomplete {
22        /// The number of additional bytes needed, at least.
23        needed: usize,
24    },
25    /// A record's CRC does not match its contents.
26    #[error("invalid CRC: {0}")]
27    InvalidCrc(String),
28    /// A record is malformed: inconsistent header values, blockettes or data payload.
29    #[error("invalid record: {0}")]
30    InvalidRecord(String),
31    /// A data encoding is not supported for the operation.
32    #[error("unsupported encoding: {0}")]
33    UnsupportedEncoding(String),
34    /// Steim compressed data is invalid.
35    #[error("invalid Steim data: {0}")]
36    Steim(String),
37    /// Data samples cannot be decoded, converted or combined as requested.
38    #[error("sample data: {0}")]
39    Samples(String),
40    /// Records cannot be packed from the given record or trace list.
41    #[error("cannot pack: {0}")]
42    Pack(String),
43    /// A time is invalid or cannot be represented.
44    #[error("invalid time: {0}")]
45    InvalidTime(String),
46    /// A source identifier is invalid or cannot be converted.
47    #[error("invalid source identifier: {0}")]
48    InvalidSourceId(String),
49    /// Extra headers are invalid JSON, too long, or a JSON Pointer operation failed.
50    #[error("extra headers: {0}")]
51    ExtraHeaders(String),
52    /// A data selection is invalid.
53    #[error("invalid selection: {0}")]
54    Selection(String),
55    /// An argument or object state does not allow the operation.
56    #[error("invalid argument: {0}")]
57    InvalidArgument(String),
58    /// An I/O error, with context added to its message.
59    #[error(transparent)]
60    Io(#[from] std::io::Error),
61    /// An error from caller code, such as a record handler; see [`Error::other`].
62    #[error(transparent)]
63    Other(Box<dyn std::error::Error + Send + Sync>),
64}
65
66impl Error {
67    /// Wrap an error from caller code, for example to fail from a record handler:
68    ///
69    /// ```
70    /// let err = libmseed::Error::other("output is full");
71    /// assert_eq!(err.to_string(), "output is full");
72    /// ```
73    pub fn other(err: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
74        Error::Other(err.into())
75    }
76
77    /// An [`Error::Io`] keeping the [`std::io::ErrorKind`] of `err` and adding `context`
78    /// to its message.
79    pub(crate) fn io(err: std::io::Error, context: impl std::fmt::Display) -> Self {
80        Error::Io(std::io::Error::new(err.kind(), format!("{context}: {err}")))
81    }
82
83    /// The same error with `context` prefixed to its message.
84    pub(crate) fn context(self, context: impl std::fmt::Display) -> Self {
85        use Error as E;
86        let add = |m: String| format!("{context}: {m}");
87        match self {
88            E::NotSeed(m) => E::NotSeed(add(m)),
89            E::OutOfRange(m) => E::OutOfRange(add(m)),
90            E::InvalidCrc(m) => E::InvalidCrc(add(m)),
91            E::InvalidRecord(m) => E::InvalidRecord(add(m)),
92            E::UnsupportedEncoding(m) => E::UnsupportedEncoding(add(m)),
93            E::Steim(m) => E::Steim(add(m)),
94            E::Samples(m) => E::Samples(add(m)),
95            E::Pack(m) => E::Pack(add(m)),
96            E::InvalidTime(m) => E::InvalidTime(add(m)),
97            E::InvalidSourceId(m) => E::InvalidSourceId(add(m)),
98            E::ExtraHeaders(m) => E::ExtraHeaders(add(m)),
99            E::Selection(m) => E::Selection(add(m)),
100            E::InvalidArgument(m) => E::InvalidArgument(add(m)),
101            E::Io(e) => Error::io(e, context),
102            e @ (E::Incomplete { .. } | E::Other(_)) => e,
103        }
104    }
105}