From 626c0221060e2e47b9a700cb58d78f333ebdaf6d Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Sat, 4 Jul 2015 08:42:03 -0400 Subject: [PATCH] Implement std::error::Error for Error --- src/error.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/error.rs b/src/error.rs index e1986d3..84e6b97 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ use ffi; use std::convert::{From, Into}; -use std::fmt::{self, Display, Formatter}; +use std::{error, fmt}; /// An error. #[derive(Debug)] @@ -77,17 +77,26 @@ impl From for Error { } } -impl Display for Error { - fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match self.message { - Some(ref message) => Display::fmt(message, formatter), - None => Display::fmt(&self.kind, formatter), + Some(ref message) => message.fmt(formatter), + _ => self.kind.fmt(formatter), } } } -impl Display for ErrorKind { - fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { +impl error::Error for Error { + fn description(&self) -> &str { + match self.message { + Some(ref message) => message, + _ => "an SQLite error", + } + } +} + +impl fmt::Display for ErrorKind { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match *self { ErrorKind::Unknown => write!(formatter, "an unknown SQLite result code"), _ => write!(formatter, "SQLite result code {}", *self as isize),