various: add Display impls for domain enums; improve contextual errors

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ia16e7e34cda6ae3e12590ea1ea9268486a6a6964
This commit is contained in:
raf 2026-03-07 16:55:43 +03:00
commit cd63eeccff
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
6 changed files with 143 additions and 77 deletions

View file

@ -105,6 +105,9 @@ pub enum PinakesError {
#[error("insufficient share permissions")]
InsufficientSharePermissions,
#[error("serialization error: {0}")]
Serialization(String),
}
impl From<rusqlite::Error> for PinakesError {
@ -121,8 +124,19 @@ impl From<tokio_postgres::Error> for PinakesError {
impl From<serde_json::Error> for PinakesError {
fn from(e: serde_json::Error) -> Self {
PinakesError::Database(format!("JSON serialization error: {}", e))
PinakesError::Serialization(e.to_string())
}
}
/// Build a closure that wraps a database error with operation context.
///
/// Usage: `stmt.execute(params).map_err(db_ctx("insert_media", media_id))?;`
pub fn db_ctx<E: std::fmt::Display>(
operation: &str,
entity: impl std::fmt::Display,
) -> impl FnOnce(E) -> PinakesError {
let context = format!("{operation} [{entity}]");
move |e| PinakesError::Database(format!("{context}: {e}"))
}
pub type Result<T> = std::result::Result<T, PinakesError>;