From 39477ac76cc67d5b2c01081e0730a781bf5fe572 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 10 Jan 2019 00:16:54 -0800 Subject: cleanups; NotFound errors --- rust/src/errors.rs | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-) (limited to 'rust/src/errors.rs') diff --git a/rust/src/errors.rs b/rust/src/errors.rs index 80535fef..95979534 100644 --- a/rust/src/errors.rs +++ b/rust/src/errors.rs @@ -113,9 +113,12 @@ impl Into for FatcatError { } impl From for FatcatError { - /// The "not found" case should be handled in user code fn from(inner: diesel::result::Error) -> FatcatError { - FatcatError::DatabaseError(inner.to_string()) + match inner { + diesel::result::Error::NotFound => FatcatError::NotFound("unknown".to_string(), "N/A".to_string()), + diesel::result::Error::DatabaseError(_, _) => FatcatError::ConstraintViolation(inner.to_string()), + _ => FatcatError::InternalError(inner.to_string()) + } } } @@ -165,34 +168,13 @@ impl From for FatcatError { if let Some(_) = error.downcast_ref::() { return error.downcast::().unwrap().into(); } - // TODO: more downcast catching? + if let Some(_) = error.downcast_ref::() { + return error.downcast::().unwrap().into(); + } + if let Some(_) = error.downcast_ref::() { + return error.downcast::().unwrap().into(); + } FatcatError::InternalError(error.to_string()) } } -/* -// Allows adding more context via a String -impl From> for FatcatError { - fn from(inner: Context) -> FatcatError { - FatcatError::InternalError(inner.context()) - } -} - -// Allows adding more context via a &str -impl From> for FatcatError { - fn from(inner: Context<&'static str>) -> FatcatError { - FatcatError::InternalError(inner.context().to_string()) - } -} -*/ - -/* XXX: -Fmt(::std::fmt::Error); -Diesel(::diesel::result::Error); -R2d2(::diesel::r2d2::Error); -Uuid(::uuid::ParseError); -Io(::std::io::Error) #[cfg(unix)]; -Serde(::serde_json::Error); -Utf8Decode(::std::string::FromUtf8Error); -StringDecode(::data_encoding::DecodeError); -*/ -- cgit v1.2.3