diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-10 00:16:54 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-10 00:16:54 -0800 |
commit | 39477ac76cc67d5b2c01081e0730a781bf5fe572 (patch) | |
tree | 1f47ef730292e7fe0d40cb4f2a6c988dd3782c6d /rust | |
parent | 67c3460d251a4e559a1126b5fe66fe996f840010 (diff) | |
download | fatcat-39477ac76cc67d5b2c01081e0730a781bf5fe572.tar.gz fatcat-39477ac76cc67d5b2c01081e0730a781bf5fe572.zip |
cleanups; NotFound errors
Diffstat (limited to 'rust')
-rw-r--r-- | rust/src/entity_crud.rs | 14 | ||||
-rw-r--r-- | 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<Self> { - 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<models::ErrorResponse> for FatcatError { } impl From<diesel::result::Error> 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<failure::Error> for FatcatError { if let Some(_) = error.downcast_ref::<std::fmt::Error>() { return error.downcast::<std::fmt::Error>().unwrap().into(); } - // TODO: more downcast catching? + if let Some(_) = error.downcast_ref::<diesel::result::Error>() { + return error.downcast::<diesel::result::Error>().unwrap().into(); + } + if let Some(_) = error.downcast_ref::<uuid::ParseError>() { + return error.downcast::<uuid::ParseError>().unwrap().into(); + } FatcatError::InternalError(error.to_string()) } } -/* -// Allows adding more context via a String -impl From<Context<String>> for FatcatError { - fn from(inner: Context<String>) -> FatcatError { - FatcatError::InternalError(inner.context()) - } -} - -// Allows adding more context via a &str -impl From<Context<&'static str>> 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); -*/ |