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/entity_crud.rs | 14 ++++++++++++-- rust/src/errors.rs | 40 +++++++++++----------------------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/rust/src/entity_crud.rs b/rust/src/entity_crud.rs index 43ed2083..0c1e29b0 100644 --- a/rust/src/entity_crud.rs +++ b/rust/src/entity_crud.rs @@ -273,7 +273,12 @@ macro_rules! generic_db_get { }, None => { // return a stub (deleted) entity if it's just deleted state - let ident_row: Self::IdentRow = $ident_table::table.find(ident.to_uuid()).first(conn)?; + let ident_row: Self::IdentRow = match $ident_table::table.find(ident.to_uuid()).first(conn) { + Ok(row) => row, + Err(diesel::result::Error::NotFound) => + Err(FatcatError::NotFound(stringify!($ident_table).to_string(), ident.to_string()))?, + Err(e) => Err(e)?, + }; if ident_row.rev_id.is_none() { Self::from_deleted_row(ident_row) } else { @@ -288,7 +293,12 @@ macro_rules! generic_db_get { macro_rules! generic_db_get_rev { ($rev_table:ident) => { fn db_get_rev(conn: &DbConn, rev_id: Uuid, hide: HideFlags) -> Result { - let rev = $rev_table::table.find(rev_id).first(conn)?; + let rev = match $rev_table::table.find(rev_id).first(conn) { + Ok(rev) => rev, + Err(diesel::result::Error::NotFound) => + Err(FatcatError::NotFound(stringify!($rev_table).to_string(), rev_id.to_string()))?, + Err(e) => Err(e)?, + }; Self::db_from_row(conn, rev, None, hide) } 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