Skip to content

Commit

Permalink
Do not unwrap rd2d errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ja573 committed Sep 3, 2024
1 parent ca80fc5 commit fbfc559
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 55 deletions.
4 changes: 2 additions & 2 deletions thoth-api/src/model/affiliation/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Crud for Affiliation {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Affiliation>> {
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(
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Crud for Affiliation {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/contribution/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Crud for Contribution {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Contribution>> {
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)
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Crud for Contribution {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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));
Expand Down
6 changes: 3 additions & 3 deletions thoth-api/src/model/contributor/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Crud for Contributor {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Contributor>> {
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 {
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Crud for Contributor {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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(
Expand Down Expand Up @@ -160,7 +160,7 @@ fn contributor_linked_publisher_ids(
contributor_id: Uuid,
db: &crate::db::PgPool,
) -> ThothResult<Vec<Uuid>> {
let mut connection = db.get().unwrap();
let mut connection = db.get()?;
crate::schema::publisher::table
.inner_join(
crate::schema::imprint::table.inner_join(
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/funding/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Crud for Funding {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Funding>> {
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)
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Crud for Funding {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/imprint/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Crud for Imprint {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Imprint>> {
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 {
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Crud for Imprint {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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));
Expand Down
6 changes: 3 additions & 3 deletions thoth-api/src/model/institution/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Crud for Institution {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Institution>> {
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 {
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Crud for Institution {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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(
Expand Down Expand Up @@ -156,7 +156,7 @@ fn institution_linked_publisher_ids(
institution_id: Uuid,
db: &crate::db::PgPool,
) -> ThothResult<Vec<Uuid>> {
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(
Expand Down
6 changes: 3 additions & 3 deletions thoth-api/src/model/issue/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Crud for Issue {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Issue>> {
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)
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Crud for Issue {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/language/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Crud for Language {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Language>> {
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)
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Crud for Language {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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));
Expand Down
8 changes: 4 additions & 4 deletions thoth-api/src/model/location/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Crud for Location {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Location>> {
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(
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Crud for Location {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions thoth-api/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ macro_rules! crud_methods {
fn from_id(db: &$crate::db::PgPool, entity_id: &Uuid) -> ThothResult<Self> {
use diesel::{QueryDsl, RunQueryDsl};

let mut connection = db.get().unwrap();
let mut connection = db.get()?;
match $entity_dsl
.find(entity_id)
.get_result::<Self>(&mut connection)
Expand All @@ -405,7 +405,7 @@ macro_rules! crud_methods {
}

fn create(db: &$crate::db::PgPool, data: &Self::NewEntity) -> ThothResult<Self> {
let mut connection = db.get().unwrap();
let mut connection = db.get()?;
match diesel::insert_into($table_dsl)
.values(data)
.get_result::<Self>(&mut connection)
Expand All @@ -425,7 +425,7 @@ macro_rules! crud_methods {
) -> ThothResult<Self> {
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)
Expand All @@ -443,7 +443,7 @@ macro_rules! crud_methods {
fn delete(self, db: &$crate::db::PgPool) -> ThothResult<Self> {
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)),
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/price/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Crud for Price {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Price>> {
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(
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Crud for Price {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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));
Expand Down
12 changes: 6 additions & 6 deletions thoth-api/src/model/publication/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Crud for Publication {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Publication>> {
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)
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Crud for Publication {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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();
Expand Down Expand Up @@ -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<WorkType> {
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::<WorkType>(&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() {
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/publisher/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Crud for Publisher {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Publisher>> {
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 {
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Crud for Publisher {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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));
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/reference/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Crud for Reference {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Reference>> {
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)
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Crud for Reference {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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();
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/series/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Crud for Series {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Series>> {
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)
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Crud for Series {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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();
Expand Down
4 changes: 2 additions & 2 deletions thoth-api/src/model/subject/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Crud for Subject {
_: Option<Self::FilterParameter3>,
) -> ThothResult<Vec<Subject>> {
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)
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Crud for Subject {
_: Option<Self::FilterParameter3>,
) -> ThothResult<i32> {
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));
Expand Down
Loading

0 comments on commit fbfc559

Please sign in to comment.