Skip to main content

zenith_session/
error.rs

1//! [`SessionError`]: the single error type for the zenith-session crate.
2
3use std::fmt;
4
5/// An error produced by zenith-session operations.
6///
7/// Mirrors the hand-rolled style of `zenith-tx`'s `TxError` — no third-party
8/// error libraries.
9#[derive(Debug, Clone, PartialEq)]
10pub struct SessionError {
11    pub message: String,
12}
13
14impl SessionError {
15    pub fn new(message: impl Into<String>) -> Self {
16        Self {
17            message: message.into(),
18        }
19    }
20}
21
22impl fmt::Display for SessionError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "zenith-session error: {}", self.message)
25    }
26}
27
28impl std::error::Error for SessionError {}
29
30impl From<std::io::Error> for SessionError {
31    fn from(e: std::io::Error) -> Self {
32        Self::new(e.to_string())
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn display_includes_message() {
42        let e = SessionError::new("something went wrong");
43        assert_eq!(e.to_string(), "zenith-session error: something went wrong");
44    }
45
46    #[test]
47    fn from_io_error() {
48        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file missing");
49        let e = SessionError::from(io_err);
50        assert!(e.message.contains("file missing"));
51    }
52}