From fbfc559bd312aab9428a89bacf9987d4b936efec Mon Sep 17 00:00:00 2001 From: Javier Arias Date: Tue, 3 Sep 2024 14:59:47 +0100 Subject: [PATCH] Do not unwrap rd2d errors --- thoth-api/src/model/affiliation/crud.rs | 4 ++-- thoth-api/src/model/contribution/crud.rs | 4 ++-- thoth-api/src/model/contributor/crud.rs | 6 +++--- thoth-api/src/model/funding/crud.rs | 4 ++-- thoth-api/src/model/imprint/crud.rs | 4 ++-- thoth-api/src/model/institution/crud.rs | 6 +++--- thoth-api/src/model/issue/crud.rs | 6 +++--- thoth-api/src/model/language/crud.rs | 4 ++-- thoth-api/src/model/location/crud.rs | 8 ++++---- thoth-api/src/model/mod.rs | 8 ++++---- thoth-api/src/model/price/crud.rs | 4 ++-- thoth-api/src/model/publication/crud.rs | 12 ++++++------ thoth-api/src/model/publisher/crud.rs | 4 ++-- thoth-api/src/model/reference/crud.rs | 4 ++-- thoth-api/src/model/series/crud.rs | 4 ++-- thoth-api/src/model/subject/crud.rs | 4 ++-- thoth-api/src/model/work/crud.rs | 10 +++++----- thoth-api/src/model/work_relation/crud.rs | 14 +++++++------- 18 files changed, 55 insertions(+), 55 deletions(-) diff --git a/thoth-api/src/model/affiliation/crud.rs b/thoth-api/src/model/affiliation/crud.rs index 20c192eb..82e60f07 100644 --- a/thoth-api/src/model/affiliation/crud.rs +++ b/thoth-api/src/model/affiliation/crud.rs @@ -36,7 +36,7 @@ impl Crud for Affiliation { _: Option, ) -> ThothResult> { use crate::schema::affiliation::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = affiliation .inner_join(crate::schema::contribution::table.inner_join( @@ -103,7 +103,7 @@ impl Crud for Affiliation { _: Option, ) -> ThothResult { use crate::schema::affiliation::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; // `SELECT COUNT(*)` in postgres returns a BIGINT, which diesel parses as i64. Juniper does // not implement i64 yet, only i32. The only sensible way, albeit shameful, to solve this diff --git a/thoth-api/src/model/contribution/crud.rs b/thoth-api/src/model/contribution/crud.rs index a53f178f..3de72b9f 100644 --- a/thoth-api/src/model/contribution/crud.rs +++ b/thoth-api/src/model/contribution/crud.rs @@ -37,7 +37,7 @@ impl Crud for Contribution { _: Option, ) -> ThothResult> { use crate::schema::contribution::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = contribution .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .select(crate::schema::contribution::all_columns) @@ -124,7 +124,7 @@ impl Crud for Contribution { _: Option, ) -> ThothResult { use crate::schema::contribution::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = contribution.into_boxed(); if !contribution_types.is_empty() { query = query.filter(contribution_type.eq_any(contribution_types)); diff --git a/thoth-api/src/model/contributor/crud.rs b/thoth-api/src/model/contributor/crud.rs index 1a845f8b..9080389e 100644 --- a/thoth-api/src/model/contributor/crud.rs +++ b/thoth-api/src/model/contributor/crud.rs @@ -38,7 +38,7 @@ impl Crud for Contributor { _: Option, ) -> ThothResult> { use crate::schema::contributor::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = contributor.into_boxed(); query = match order.field { @@ -102,7 +102,7 @@ impl Crud for Contributor { _: Option, ) -> ThothResult { use crate::schema::contributor::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = contributor.into_boxed(); if let Some(filter) = filter { query = query.filter( @@ -160,7 +160,7 @@ fn contributor_linked_publisher_ids( contributor_id: Uuid, db: &crate::db::PgPool, ) -> ThothResult> { - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; crate::schema::publisher::table .inner_join( crate::schema::imprint::table.inner_join( diff --git a/thoth-api/src/model/funding/crud.rs b/thoth-api/src/model/funding/crud.rs index 0b1bfa6a..e61e5d44 100644 --- a/thoth-api/src/model/funding/crud.rs +++ b/thoth-api/src/model/funding/crud.rs @@ -34,7 +34,7 @@ impl Crud for Funding { _: Option, ) -> ThothResult> { use crate::schema::funding::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = funding .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .select(crate::schema::funding::all_columns) @@ -110,7 +110,7 @@ impl Crud for Funding { _: Option, ) -> ThothResult { use crate::schema::funding::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; // `SELECT COUNT(*)` in postgres returns a BIGINT, which diesel parses as i64. Juniper does // not implement i64 yet, only i32. The only sensible way, albeit shameful, to solve this diff --git a/thoth-api/src/model/imprint/crud.rs b/thoth-api/src/model/imprint/crud.rs index 7f127576..65f788ff 100644 --- a/thoth-api/src/model/imprint/crud.rs +++ b/thoth-api/src/model/imprint/crud.rs @@ -38,7 +38,7 @@ impl Crud for Imprint { _: Option, ) -> ThothResult> { use crate::schema::imprint::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = imprint.into_boxed(); query = match order.field { @@ -99,7 +99,7 @@ impl Crud for Imprint { _: Option, ) -> ThothResult { use crate::schema::imprint::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = imprint.into_boxed(); if !publishers.is_empty() { query = query.filter(publisher_id.eq_any(publishers)); diff --git a/thoth-api/src/model/institution/crud.rs b/thoth-api/src/model/institution/crud.rs index 10d8ee54..d5ddbe49 100644 --- a/thoth-api/src/model/institution/crud.rs +++ b/thoth-api/src/model/institution/crud.rs @@ -38,7 +38,7 @@ impl Crud for Institution { _: Option, ) -> ThothResult> { use crate::schema::institution::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = institution.into_boxed(); query = match order.field { @@ -98,7 +98,7 @@ impl Crud for Institution { _: Option, ) -> ThothResult { use crate::schema::institution::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = institution.into_boxed(); if let Some(filter) = filter { query = query.filter( @@ -156,7 +156,7 @@ fn institution_linked_publisher_ids( institution_id: Uuid, db: &crate::db::PgPool, ) -> ThothResult> { - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let publishers_via_affiliation = crate::schema::publisher::table .inner_join(crate::schema::imprint::table.inner_join( crate::schema::work::table.inner_join( diff --git a/thoth-api/src/model/issue/crud.rs b/thoth-api/src/model/issue/crud.rs index 20e1ac0a..2fee3320 100644 --- a/thoth-api/src/model/issue/crud.rs +++ b/thoth-api/src/model/issue/crud.rs @@ -34,7 +34,7 @@ impl Crud for Issue { _: Option, ) -> ThothResult> { use crate::schema::issue::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = issue .inner_join(crate::schema::series::table.inner_join(crate::schema::imprint::table)) .select(crate::schema::issue::all_columns) @@ -94,7 +94,7 @@ impl Crud for Issue { _: Option, ) -> ThothResult { use crate::schema::issue::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; // `SELECT COUNT(*)` in postgres returns a BIGINT, which diesel parses as i64. Juniper does // not implement i64 yet, only i32. The only sensible way, albeit shameful, to solve this @@ -146,7 +146,7 @@ impl PatchIssue { fn issue_imprints_match(work_id: Uuid, series_id: Uuid, db: &crate::db::PgPool) -> ThothResult<()> { use diesel::prelude::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let series_imprint = crate::schema::series::table .select(crate::schema::series::imprint_id) .filter(crate::schema::series::series_id.eq(series_id)) diff --git a/thoth-api/src/model/language/crud.rs b/thoth-api/src/model/language/crud.rs index 31284e3f..fe0d27d5 100644 --- a/thoth-api/src/model/language/crud.rs +++ b/thoth-api/src/model/language/crud.rs @@ -37,7 +37,7 @@ impl Crud for Language { _: Option, ) -> ThothResult> { use crate::schema::language::dsl; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = dsl::language .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .select(crate::schema::language::all_columns) @@ -104,7 +104,7 @@ impl Crud for Language { _: Option, ) -> ThothResult { use crate::schema::language::dsl; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = dsl::language.into_boxed(); if !language_codes.is_empty() { query = query.filter(dsl::language_code.eq_any(language_codes)); diff --git a/thoth-api/src/model/location/crud.rs b/thoth-api/src/model/location/crud.rs index b9c73a5d..1692dce4 100644 --- a/thoth-api/src/model/location/crud.rs +++ b/thoth-api/src/model/location/crud.rs @@ -36,7 +36,7 @@ impl Crud for Location { _: Option, ) -> ThothResult> { use crate::schema::location::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = location .inner_join(crate::schema::publication::table.inner_join( @@ -107,7 +107,7 @@ impl Crud for Location { _: Option, ) -> ThothResult { use crate::schema::location::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = location.into_boxed(); if !location_platforms.is_empty() { query = query.filter(location_platform.eq_any(location_platforms)); @@ -152,7 +152,7 @@ impl NewLocation { use crate::schema::location::dsl::*; use diesel::prelude::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let canonical_count = location .filter(publication_id.eq(self.publication_id)) .filter(canonical) @@ -205,7 +205,7 @@ fn location_canonical_record_complete( use crate::model::publication::PublicationType; use diesel::prelude::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let publication_type = crate::schema::publication::table .select(crate::schema::publication::publication_type) .filter(crate::schema::publication::publication_id.eq(publication_id)) diff --git a/thoth-api/src/model/mod.rs b/thoth-api/src/model/mod.rs index 9821c61f..2da0053d 100644 --- a/thoth-api/src/model/mod.rs +++ b/thoth-api/src/model/mod.rs @@ -394,7 +394,7 @@ macro_rules! crud_methods { fn from_id(db: &$crate::db::PgPool, entity_id: &Uuid) -> ThothResult { use diesel::{QueryDsl, RunQueryDsl}; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; match $entity_dsl .find(entity_id) .get_result::(&mut connection) @@ -405,7 +405,7 @@ macro_rules! crud_methods { } fn create(db: &$crate::db::PgPool, data: &Self::NewEntity) -> ThothResult { - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; match diesel::insert_into($table_dsl) .values(data) .get_result::(&mut connection) @@ -425,7 +425,7 @@ macro_rules! crud_methods { ) -> ThothResult { use diesel::{Connection, QueryDsl, RunQueryDsl}; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; connection.transaction(|connection| { match diesel::update($entity_dsl.find(&self.pk())) .set(data) @@ -443,7 +443,7 @@ macro_rules! crud_methods { fn delete(self, db: &$crate::db::PgPool) -> ThothResult { use diesel::{QueryDsl, RunQueryDsl}; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; match diesel::delete($entity_dsl.find(&self.pk())).execute(&mut connection) { Ok(_) => Ok(self), Err(e) => Err(ThothError::from(e)), diff --git a/thoth-api/src/model/price/crud.rs b/thoth-api/src/model/price/crud.rs index f22e17a2..c33006af 100644 --- a/thoth-api/src/model/price/crud.rs +++ b/thoth-api/src/model/price/crud.rs @@ -34,7 +34,7 @@ impl Crud for Price { _: Option, ) -> ThothResult> { use crate::schema::price::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = price .inner_join(crate::schema::publication::table.inner_join( @@ -97,7 +97,7 @@ impl Crud for Price { _: Option, ) -> ThothResult { use crate::schema::price::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = price.into_boxed(); if !currency_codes.is_empty() { query = query.filter(currency_code.eq_any(currency_codes)); diff --git a/thoth-api/src/model/publication/crud.rs b/thoth-api/src/model/publication/crud.rs index b14df5dc..894fc2b8 100644 --- a/thoth-api/src/model/publication/crud.rs +++ b/thoth-api/src/model/publication/crud.rs @@ -37,7 +37,7 @@ impl Crud for Publication { _: Option, ) -> ThothResult> { use crate::schema::publication::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = publication .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .select(crate::schema::publication::all_columns) @@ -135,7 +135,7 @@ impl Crud for Publication { _: Option, ) -> ThothResult { use crate::schema::publication::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = publication .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .into_boxed(); @@ -191,18 +191,18 @@ pub trait PublicationValidation where Self: PublicationProperties, { - fn work_type(&self, db: &crate::db::PgPool) -> WorkType { + fn work_type(&self, db: &crate::db::PgPool) -> ThothResult { use diesel::prelude::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; crate::schema::work::table .select(crate::schema::work::work_type) .filter(crate::schema::work::work_id.eq(self.work_id())) .first::(&mut connection) - .expect("Error loading work type for publication") + .map_err(Into::into) } fn chapter_error(&self, db: &crate::db::PgPool) -> ThothResult<()> { - if self.work_type(db) == WorkType::BookChapter { + if self.work_type(db)? == WorkType::BookChapter { // If a publication's work is of type Book Chapter, // it cannot have an ISBN, or any dimensions. if self.isbn().is_some() { diff --git a/thoth-api/src/model/publisher/crud.rs b/thoth-api/src/model/publisher/crud.rs index 1ee2bceb..012822ea 100644 --- a/thoth-api/src/model/publisher/crud.rs +++ b/thoth-api/src/model/publisher/crud.rs @@ -38,7 +38,7 @@ impl Crud for Publisher { _: Option, ) -> ThothResult> { use crate::schema::publisher::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = publisher.into_boxed(); query = match order.field { @@ -96,7 +96,7 @@ impl Crud for Publisher { _: Option, ) -> ThothResult { use crate::schema::publisher::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = publisher.into_boxed(); if !publishers.is_empty() { query = query.filter(publisher_id.eq_any(publishers)); diff --git a/thoth-api/src/model/reference/crud.rs b/thoth-api/src/model/reference/crud.rs index 40e6a40c..f1a9e325 100644 --- a/thoth-api/src/model/reference/crud.rs +++ b/thoth-api/src/model/reference/crud.rs @@ -38,7 +38,7 @@ impl Crud for Reference { _: Option, ) -> ThothResult> { use crate::schema::reference::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = reference .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .select(crate::schema::reference::all_columns) @@ -191,7 +191,7 @@ impl Crud for Reference { _: Option, ) -> ThothResult { use crate::schema::reference::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = reference .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .into_boxed(); diff --git a/thoth-api/src/model/series/crud.rs b/thoth-api/src/model/series/crud.rs index f9a6bfec..29a987d5 100644 --- a/thoth-api/src/model/series/crud.rs +++ b/thoth-api/src/model/series/crud.rs @@ -38,7 +38,7 @@ impl Crud for Series { _: Option, ) -> ThothResult> { use crate::schema::series::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = series .inner_join(crate::schema::imprint::table) .select(crate::schema::series::all_columns) @@ -121,7 +121,7 @@ impl Crud for Series { _: Option, ) -> ThothResult { use crate::schema::series::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = series .inner_join(crate::schema::imprint::table) .into_boxed(); diff --git a/thoth-api/src/model/subject/crud.rs b/thoth-api/src/model/subject/crud.rs index 37221343..b8504bda 100644 --- a/thoth-api/src/model/subject/crud.rs +++ b/thoth-api/src/model/subject/crud.rs @@ -36,7 +36,7 @@ impl Crud for Subject { _: Option, ) -> ThothResult> { use crate::schema::subject::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = subject .inner_join(crate::schema::work::table.inner_join(crate::schema::imprint::table)) .select(crate::schema::subject::all_columns) @@ -104,7 +104,7 @@ impl Crud for Subject { _: Option, ) -> ThothResult { use crate::schema::subject::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = subject.into_boxed(); if !subject_types.is_empty() { query = query.filter(subject_type.eq_any(subject_types)); diff --git a/thoth-api/src/model/work/crud.rs b/thoth-api/src/model/work/crud.rs index adec7b28..3fc25ef8 100644 --- a/thoth-api/src/model/work/crud.rs +++ b/thoth-api/src/model/work/crud.rs @@ -23,7 +23,7 @@ impl Work { use crate::schema::work::dsl; use diesel::sql_types::Nullable; use diesel::sql_types::Text; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; // Allow case-insensitive searching (DOIs in database may have mixed casing) define_sql_function!(fn lower(x: Nullable) -> Nullable); let mut query = dsl::work @@ -39,7 +39,7 @@ impl Work { pub fn can_update_imprint(&self, db: &crate::db::PgPool) -> ThothResult<()> { use crate::schema::issue::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; // `SELECT COUNT(*)` in postgres returns a BIGINT, which diesel parses as i64. Juniper does // not implement i64 yet, only i32. The only sensible way, albeit shameful, to solve this // is converting i64 to string and then parsing it as i32. This should work until we reach @@ -63,7 +63,7 @@ impl Work { pub fn can_be_chapter(&self, db: &crate::db::PgPool) -> ThothResult<()> { use crate::schema::publication::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let isbn_count = publication .filter(work_id.eq(self.work_id)) .filter(isbn.is_not_null()) @@ -129,7 +129,7 @@ impl Crud for Work { updated_at_with_relations: Option, ) -> ThothResult> { use crate::schema::work::dsl; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = dsl::work .inner_join(crate::schema::imprint::table) .select(crate::schema::work::all_columns) @@ -333,7 +333,7 @@ impl Crud for Work { updated_at_with_relations: Option, ) -> ThothResult { use crate::schema::work::dsl; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = dsl::work .inner_join(crate::schema::imprint::table) .into_boxed(); diff --git a/thoth-api/src/model/work_relation/crud.rs b/thoth-api/src/model/work_relation/crud.rs index d80b6b9e..9f5fda3f 100644 --- a/thoth-api/src/model/work_relation/crud.rs +++ b/thoth-api/src/model/work_relation/crud.rs @@ -37,7 +37,7 @@ impl Crud for WorkRelation { _: Option, ) -> ThothResult> { use crate::schema::work_relation::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = work_relation .select(crate::schema::work_relation::all_columns) .into_boxed(); @@ -97,7 +97,7 @@ impl Crud for WorkRelation { _: Option, ) -> ThothResult { use crate::schema::work_relation::dsl::*; - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; let mut query = work_relation.into_boxed(); if !relation_types.is_empty() { query = query.filter(relation_type.eq_any(relation_types)); @@ -117,7 +117,7 @@ impl Crud for WorkRelation { // as we need to execute multiple statements in the same transaction. // This function recreates the `crud_methods!` from_id() logic. fn from_id(db: &crate::db::PgPool, entity_id: &Uuid) -> ThothResult { - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; match work_relation::table .find(entity_id) .get_result::(&mut connection) @@ -130,7 +130,7 @@ impl Crud for WorkRelation { fn create(db: &crate::db::PgPool, data: &NewWorkRelation) -> ThothResult { // For each Relator - Relationship - Related record we create, we must also // create the corresponding Related - InverseRelationship - Relator record. - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; // We need to determine an appropriate relation_ordinal for the inverse record. // Find the current highest ordinal for the relevant work and type. // This will return `None` if no records with this work and type already exist. @@ -183,7 +183,7 @@ impl Crud for WorkRelation { }; // Execute both updates within the same transaction, // because if one fails, both need to be reverted. - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; connection.transaction(|connection| { diesel::update(work_relation::table.find(inverse_work_relation.work_relation_id)) .set(inverse_data) @@ -209,7 +209,7 @@ impl Crud for WorkRelation { let inverse_work_relation = self.get_inverse(db)?; // Execute both deletions within the same transaction, // because if one fails, both need to be reverted. - let mut connection = db.get().unwrap(); + let mut connection = db.get()?; connection.transaction(|connection| { diesel::delete(work_relation::table.find(inverse_work_relation.work_relation_id)) .execute(connection)?; @@ -255,7 +255,7 @@ impl WorkRelation { .eq(self.related_work_id) .and(work_relation::related_work_id.eq(self.relator_work_id)), ) - .first::(&mut db.get().unwrap()) + .first::(&mut db.get()?) { // The inverse record should have the inverse relation_type, // but this cannot be enforced by the database. Test for data integrity.