2018-09-26 16:53:04 +02:00
|
|
|
//! This module defines error types and conversions for issue that can
|
|
|
|
//! occur while dealing with persisted state machines.
|
|
|
|
|
|
|
|
use std::result;
|
2018-12-13 15:01:05 +01:00
|
|
|
use std::fmt;
|
2018-12-13 14:40:59 +01:00
|
|
|
use uuid::Uuid;
|
2018-12-13 15:01:05 +01:00
|
|
|
use std::error::Error as StdError;
|
2018-12-13 14:40:59 +01:00
|
|
|
|
|
|
|
// errors to chain:
|
|
|
|
use postgres::Error as PgError;
|
2018-12-13 16:53:45 +01:00
|
|
|
use r2d2_postgres::r2d2::Error as PoolError;
|
|
|
|
use serde_json::Error as JsonError;
|
2018-09-26 16:53:04 +02:00
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
2018-09-26 17:28:45 +02:00
|
|
|
#[derive(Debug)]
|
2018-12-13 14:40:59 +01:00
|
|
|
pub struct Error {
|
|
|
|
pub kind: ErrorKind,
|
|
|
|
pub context: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ErrorKind {
|
|
|
|
/// Errors occuring during JSON serialization of FSM types.
|
|
|
|
Serialization(String),
|
|
|
|
|
|
|
|
/// Errors occuring during communication with the database.
|
|
|
|
Database(String),
|
|
|
|
|
2018-12-13 16:53:45 +01:00
|
|
|
/// Errors with the database connection pool.
|
|
|
|
DBPool(String),
|
|
|
|
|
2018-12-13 14:40:59 +01:00
|
|
|
/// State machine could not be found.
|
|
|
|
FSMNotFound(Uuid),
|
|
|
|
|
|
|
|
/// Action could not be found.
|
|
|
|
ActionNotFound(Uuid),
|
|
|
|
}
|
|
|
|
|
2018-12-13 15:01:05 +01:00
|
|
|
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),
|
|
|
|
|
2018-12-13 16:53:45 +01:00
|
|
|
DBPool(err) =>
|
|
|
|
format!("Database connection pool error: {}", err),
|
|
|
|
|
2018-12-13 15:01:05 +01:00
|
|
|
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 {}
|
|
|
|
|
2018-12-13 14:40:59 +01:00
|
|
|
impl <E: Into<ErrorKind>> From<E> for Error {
|
|
|
|
fn from(err: E) -> Error {
|
|
|
|
Error {
|
|
|
|
kind: err.into(),
|
|
|
|
context: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<JsonError> for ErrorKind {
|
|
|
|
fn from(err: JsonError) -> ErrorKind {
|
|
|
|
ErrorKind::Serialization(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PgError> for ErrorKind {
|
|
|
|
fn from(err: PgError) -> ErrorKind {
|
|
|
|
ErrorKind::Database(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:53:45 +01:00
|
|
|
impl From<PoolError> for ErrorKind {
|
|
|
|
fn from(err: PoolError) -> ErrorKind {
|
|
|
|
ErrorKind::DBPool(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 14:40:59 +01:00
|
|
|
/// Helper trait that makes it possible to supply contextual
|
|
|
|
/// information with an error.
|
|
|
|
pub trait ResultExt<T> {
|
2018-12-13 15:01:05 +01:00
|
|
|
fn context<C: fmt::Display>(self, ctx: C) -> Result<T>;
|
2018-12-13 14:40:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <T, E: Into<Error>> ResultExt<T> for result::Result<T, E> {
|
2018-12-13 15:01:05 +01:00
|
|
|
fn context<C: fmt::Display>(self, ctx: C) -> Result<T> {
|
2018-12-13 14:40:59 +01:00
|
|
|
self.map_err(|err| Error {
|
|
|
|
context: Some(format!("{}", ctx)),
|
|
|
|
.. err.into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|