#[derive(Debug, thiserror::Error)]
pub enum MemoryError {
#[error("{context}: {source}")]
Sqlite {
context: String,
#[source]
source: sqlx::Error,
},
#[error("{context}: {source}")]
Io {
context: String,
#[source]
source: std::io::Error,
},
#[error("{context}: {source}")]
Serde {
context: String,
#[source]
source: serde_json::Error,
},
#[error("{0}")]
Logic(String),
}
impl MemoryError {
pub fn sqlite(context: impl Into<String>, source: sqlx::Error) -> Self {
Self::Sqlite {
context: context.into(),
source,
}
}
pub fn io(context: impl Into<String>, source: std::io::Error) -> Self {
Self::Io {
context: context.into(),
source,
}
}
pub fn serde(context: impl Into<String>, source: serde_json::Error) -> Self {
Self::Serde {
context: context.into(),
source,
}
}
pub fn logic(msg: impl Into<String>) -> Self {
Self::Logic(msg.into())
}
}
impl From<MemoryError> for kernex_core::error::KernexError {
fn from(err: MemoryError) -> Self {
kernex_core::error::KernexError::store(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sqlite_display_includes_context_and_source() {
let inner = sqlx::Error::PoolTimedOut;
let err = MemoryError::sqlite("acquire conn", inner);
let msg = format!("{err}");
assert!(msg.contains("acquire conn"), "msg was {msg:?}");
assert!(msg.contains("pool"), "msg was {msg:?}");
}
#[test]
fn logic_display_passthrough() {
let err = MemoryError::logic("session not found");
assert_eq!(format!("{err}"), "session not found");
}
}