database: Add error for missing items

This commit is contained in:
Elias Projahn 2021-04-25 22:48:25 +02:00
parent d9746c8ab4
commit ac14beb665
4 changed files with 21 additions and 46 deletions

View file

@ -10,6 +10,9 @@ pub enum Error {
#[error(transparent)] #[error(transparent)]
QueryError(#[from] diesel::result::Error), QueryError(#[from] diesel::result::Error),
#[error("Missing item dependency ({0} {1})")]
MissingItem(&'static str, String),
#[error(transparent)] #[error(transparent)]
SendError(#[from] std::sync::mpsc::SendError<super::thread::Action>), SendError(#[from] std::sync::mpsc::SendError<super::thread::Action>),

View file

@ -256,22 +256,18 @@ impl Database {
let recording = self let recording = self
.get_recording(&recording_id)? .get_recording(&recording_id)?
.ok_or(Error::Other(format!( .ok_or(Error::MissingItem("recording", recording_id))?;
"Failed to get recording ({}) for track ({}).",
recording_id,
row.id,
)))?;
let mut part_indices = Vec::new(); let mut part_indices = Vec::new();
let work_parts = row let work_parts = row.work_parts.split(',');
.work_parts
.split(',');
for part_index in work_parts { for part_index in work_parts {
if !part_index.is_empty() { if !part_index.is_empty() {
let index = str::parse(part_index) let index = str::parse(part_index).or(Err(Error::Other(format!(
.or(Err(Error::Other(format!("Failed to parse part index from '{}'.", row.work_parts))))?; "Failed to parse part index from '{}'.",
row.work_parts
))))?;
part_indices.push(index); part_indices.push(index);
} }

View file

@ -218,47 +218,31 @@ impl Database {
person: match row.person { person: match row.person {
Some(id) => Some( Some(id) => Some(
self.get_person(&id)? self.get_person(&id)?
.ok_or(Error::Other(format!( .ok_or(Error::MissingItem("person", id))?,
"Failed to get person ({}) for recording ({}).",
id,
row.id,
)))?
), ),
None => None, None => None,
}, },
ensemble: match row.ensemble { ensemble: match row.ensemble {
Some(id) => Some( Some(id) => Some(
self.get_ensemble(&id)? self.get_ensemble(&id)?
.ok_or(Error::Other(format!( .ok_or(Error::MissingItem("ensemble", id))?,
"Failed to get ensemble ({}) for recording ({}).",
id,
row.id,
)))?
), ),
None => None, None => None,
}, },
role: match row.role { role: match row.role {
Some(id) => Some( Some(id) => Some(
self.get_instrument(&id)? self.get_instrument(&id)?
.ok_or(Error::Other(format!( .ok_or(Error::MissingItem("instrument", id))?,
"Failed to get instrument ({}) for recording ({}).",
id,
row.id,
)))?
), ),
None => None, None => None,
}, },
}); });
} }
let work_id = &row.work; let work_id = row.work;
let work = self let work = self
.get_work(work_id)? .get_work(&work_id)?
.ok_or(Error::Other(format!( .ok_or(Error::MissingItem("work", work_id))?;
"Failed to get work ({}) for recording ({}).",
work_id,
row.id,
)))?;
let recording_description = Recording { let recording_description = Recording {
id: row.id, id: row.id,

View file

@ -206,14 +206,10 @@ impl Database {
.load::<InstrumentationRow>(&self.connection)?; .load::<InstrumentationRow>(&self.connection)?;
for instrumentation in instrumentations { for instrumentation in instrumentations {
let id = &instrumentation.instrument; let id = instrumentation.instrument;
instruments.push( instruments.push(
self.get_instrument(id)? self.get_instrument(&id)?
.ok_or(Error::Other(format!( .ok_or(Error::MissingItem("instrument", id))?,
"Failed to get instrument ({}) for work ({}).",
id,
row.id,
)))?
); );
} }
@ -242,14 +238,10 @@ impl Database {
}); });
} }
let person_id = &row.composer; let person_id = row.composer;
let person = self let person = self
.get_person(person_id)? .get_person(&person_id)?
.ok_or(Error::Other(format!( .ok_or(Error::MissingItem("person", person_id))?;
"Failed to get person ({}) for work ({}).",
person_id,
row.id,
)))?;
Ok(Work { Ok(Work {
id: row.id, id: row.id,