//! This module defines error types and conversions for issue that can //! occur while dealing with persisted state machines. use std::error::Error as StdError; use std::{fmt, result}; use uuid::Uuid; // errors to chain: use postgres::Error as PgError; use r2d2_postgres::r2d2::Error as PoolError; use serde_json::Error as JsonError; pub type Result = result::Result; #[derive(Debug)] pub struct Error { pub kind: ErrorKind, pub context: Option, } #[derive(Debug)] pub enum ErrorKind { /// Errors occuring during JSON serialization of FSM types. Serialization(String), /// Errors occuring during communication with the database. Database(String), /// Errors with the database connection pool. DBPool(String), /// State machine could not be found. FSMNotFound(Uuid), /// Action could not be found. ActionNotFound(Uuid), } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use ErrorKind::*; let msg = match &self.kind { Serialization(err) => format!("JSON serialization error: {}", err), Database(err) => format!("PostgreSQL error: {}", err), DBPool(err) => format!("Database connection pool error: {}", err), FSMNotFound(id) => format!("FSM with ID {} not found", id), ActionNotFound(id) => format!("Action with ID {} not found", id), }; match &self.context { None => write!(f, "{}", msg), Some(ctx) => write!(f, "{}: {}", ctx, msg), } } } impl StdError for Error {} impl> From for Error { fn from(err: E) -> Error { Error { kind: err.into(), context: None, } } } impl From for ErrorKind { fn from(err: JsonError) -> ErrorKind { ErrorKind::Serialization(err.to_string()) } } impl From for ErrorKind { fn from(err: PgError) -> ErrorKind { ErrorKind::Database(err.to_string()) } } impl From for ErrorKind { fn from(err: PoolError) -> ErrorKind { ErrorKind::DBPool(err.to_string()) } } /// Helper trait that makes it possible to supply contextual /// information with an error. pub trait ResultExt { fn context(self, ctx: C) -> Result; } impl> ResultExt for result::Result { fn context(self, ctx: C) -> Result { self.map_err(|err| Error { context: Some(format!("{}", ctx)), ..err.into() }) } }