Skip to content

Instantly share code, notes, and snippets.

@pjenvey
Created January 23, 2019 18:33
Show Gist options
  • Save pjenvey/b040c87f7eafc340d01e6028ce3508a8 to your computer and use it in GitHub Desktop.
Save pjenvey/b040c87f7eafc340d01e6028ce3508a8 to your computer and use it in GitHub Desktop.
use failure::{Context, Fail};
use rusqlite;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
inner: Context<ErrorKind>,
}
#[derive(Debug, Fail)]
pub enum ErrorKind {
#[fail(display = "Error executing SQL: {}", _0)]
SqlError(#[fail(cause)] rusqlite::Error),
}
impl Error {
#[inline]
pub fn kind(&self) -> &ErrorKind {
self.inner.get_context()
}
}
impl From<ErrorKind> for Error {
#[inline]
fn from(kind: ErrorKind) -> Error {
Context::new(kind).into()
}
}
impl From<Context<ErrorKind>> for Error {
fn from(inner: Context<ErrorKind>) -> Error {
Error { inner }
}
}
macro_rules! impl_from_error {
($(($variant:ident, $type:ty)),+) => ($(
impl From<$type> for ErrorKind {
#[inline]
fn from(e: $type) -> ErrorKind {
ErrorKind::$variant(e)
}
}
impl From<$type> for Error {
#[inline]
fn from(e: $type) -> Error {
ErrorKind::from(e).into()
}
}
)*);
}
impl_from_error! {
(SqlError, rusqlite::Error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment