whycodes_session/
error.rs1use thiserror::Error;
2
3#[derive(Debug, Error)]
5pub enum SessionError {
6 #[error("{0}")]
7 Message(String),
8 #[error(transparent)]
9 Io(#[from] std::io::Error),
10 #[error(transparent)]
11 Json(#[from] serde_json::Error),
12 #[error(transparent)]
13 Storage(#[from] whycodes_storage::StorageError),
14 #[error(transparent)]
15 Chrono(#[from] chrono::ParseError),
16}
17
18impl SessionError {
19 pub fn msg(s: impl Into<String>) -> Self {
20 Self::Message(s.into())
21 }
22}
23
24pub type Result<T> = std::result::Result<T, SessionError>;
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn message_and_io_display() {
32 let msg = SessionError::msg("no messages");
33 assert_eq!(msg.to_string(), "no messages");
34 let io = SessionError::from(std::io::Error::other("disk"));
35 assert!(io.to_string().contains("disk"));
36 let json = SessionError::from(serde_json::from_str::<i32>("nope").unwrap_err());
37 assert!(!json.to_string().is_empty());
38 let chrono = SessionError::from(chrono::DateTime::parse_from_rfc3339("bad").unwrap_err());
39 assert!(!chrono.to_string().is_empty());
40 let storage = SessionError::from(whycodes_storage::StorageError::from(
41 rusqlite::Error::InvalidQuery,
42 ));
43 assert!(!storage.to_string().is_empty());
44 }
45}