From f02e12c63a7747b14a4fe21fb65cfc9159deea27 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 13 Jan 2022 06:57:46 +0000 Subject: [PATCH] Add ability to convert context errors to generic errors --- src/ctx/reason.rs | 13 ++++++++++--- src/err/mod.rs | 6 ++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/ctx/reason.rs b/src/ctx/reason.rs index 4ac2632b..9d96db96 100644 --- a/src/ctx/reason.rs +++ b/src/ctx/reason.rs @@ -1,4 +1,4 @@ -use std::error::Error; +use crate::err::Error; use std::fmt; use std::io; @@ -8,8 +8,6 @@ pub enum Reason { Canceled, } -impl Error for Reason {} - impl fmt::Display for Reason { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -19,6 +17,15 @@ impl fmt::Display for Reason { } } +impl From for Error { + fn from(reason: Reason) -> Error { + match reason { + Reason::Timedout => Error::TimeoutError, + Reason::Canceled => Error::CancelledError, + } + } +} + impl From for io::Error { fn from(reason: Reason) -> Self { let kind = match reason { diff --git a/src/err/mod.rs b/src/err/mod.rs index 047098b8..149d25b0 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -15,6 +15,12 @@ pub enum Error { #[error("Specify some SQL code to execute")] EmptyError, + #[error("The query failed to complete in time")] + TimeoutError, + + #[error("The query was cancelled before completion")] + CancelledError, + #[error("Parse error at position {pos} when parsing '{sql}'")] ParseError { pos: usize,