From 5aac6ec1a46a64b810f4695de968a10cab000914 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 29 Jan 2019 12:50:39 -0800 Subject: better database NotFound error propagation --- rust/src/editing_crud.rs | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'rust/src/editing_crud.rs') diff --git a/rust/src/editing_crud.rs b/rust/src/editing_crud.rs index 505bc6b4..405afcc0 100644 --- a/rust/src/editing_crud.rs +++ b/rust/src/editing_crud.rs @@ -33,7 +33,15 @@ pub trait EditorCrud { } impl EditorCrud for Editor { fn db_get(conn: &DbConn, editor_id: FatcatId) -> Result { - let editor: EditorRow = editor::table.find(editor_id.to_uuid()).get_result(conn)?; + let editor: EditorRow = match editor::table.find(editor_id.to_uuid()).get_result(conn) { + Ok(ed) => ed, + Err(diesel::result::Error::NotFound) => { + return Err( + FatcatError::NotFound("editor".to_string(), editor_id.to_string()).into(), + ); + } + other => other?, + }; Ok(editor) } @@ -105,10 +113,21 @@ impl EditgroupCrud for Editgroup { conn: &DbConn, editgroup_id: FatcatId, ) -> Result<(EditgroupRow, Option)> { - let (eg_row, cl_row): (EditgroupRow, Option) = editgroup::table + let (eg_row, cl_row): (EditgroupRow, Option) = match editgroup::table .left_outer_join(changelog::table) .filter(editgroup::id.eq(editgroup_id.to_uuid())) - .first(conn)?; + .first(conn) + { + Ok(eg) => eg, + Err(diesel::result::Error::NotFound) => { + return Err(FatcatError::NotFound( + "editgroup".to_string(), + editgroup_id.to_string(), + ) + .into()); + } + other => other?, + }; ensure!( cl_row.is_some() == eg_row.is_accepted, @@ -277,9 +296,20 @@ pub trait EditgroupAnnotationCrud { impl EditgroupAnnotationCrud for EditgroupAnnotation { fn db_get(conn: &DbConn, annotation_id: Uuid) -> Result { - let row: EditgroupAnnotationRow = editgroup_annotation::table + let row: EditgroupAnnotationRow = match editgroup_annotation::table .find(annotation_id) - .get_result(conn)?; + .get_result(conn) + { + Ok(ea) => ea, + Err(diesel::result::Error::NotFound) => { + return Err(FatcatError::NotFound( + "editgroup_annotation".to_string(), + annotation_id.to_string(), + ) + .into()); + } + other => other?, + }; Ok(row) } -- cgit v1.2.3