summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-09 06:48:04 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-09 06:48:04 -0400
commit5da3990177a1516dd2990f7b6e1b417d1c622d92 (patch)
tree3318dbb140a4fd6457614945d7493625389ba65e
parent0f6edf2c76f21a5f5834bd8b7bc85217ff8c8f18 (diff)
add: LinoError
-rw-r--r--src/error.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..649021e
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,57 @@
+use std;
+use std::fmt;
+use std::convert::From;
+use rusqlite;
+use iron::{self, Response, IronError, status};
+use iron::headers::ContentType;
+use iron::modifiers::Header;
+
+#[derive(Debug)]
+pub enum LinoError {
+ DbError(rusqlite::Error),
+ NotFound(String),
+}
+
+pub type Result<T> = std::result::Result<T, LinoError>;
+
+impl fmt::Display for LinoError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match *self {
+ LinoError::DbError(ref err) => err.fmt(f),
+ LinoError::NotFound(ref x) => write!(f, "Could not find {}", x),
+ }
+ }
+}
+
+impl std::error::Error for LinoError {
+ fn description(&self) -> &str {
+ match *self {
+ LinoError::DbError(ref err) => err.description(),
+ LinoError::NotFound(_) => "not found",
+ }
+ }
+
+ fn cause(&self) -> Option<&std::error::Error> {
+ match *self {
+ LinoError::DbError(ref err) => Some(err),
+ LinoError::NotFound(_) => None,
+ }
+ }
+}
+
+impl From<rusqlite::Error> for LinoError {
+ fn from(err: rusqlite::Error) -> LinoError {
+ LinoError::DbError(err)
+ }
+}
+
+impl From<LinoError> for IronError {
+ fn from(err: LinoError) -> IronError {
+ let code = match err {
+ LinoError::NotFound(_) => status::NotFound,
+ _ => status::InternalServerError,
+ };
+ let description = format!("{:?}", err);
+ IronError::new(err, (code, Header(ContentType::html()), description))
+ }
+}