Remove panics, cleaner code
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Lucien Cartier-Tilet 2023-01-17 01:52:56 +01:00
parent 1eb31f8e1e
commit b5dfdee453
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
2 changed files with 36 additions and 50 deletions

View File

@ -72,17 +72,14 @@ impl Language {
relationship: AgentLanguageRelation, relationship: AgentLanguageRelation,
) -> Result<Vec<User>, DatabaseError> { ) -> Result<Vec<User>, DatabaseError> {
use schema::langandagents::dsl; use schema::langandagents::dsl;
match &mut db.conn() { let conn = &mut db.conn()?;
Ok(conn) => Ok(dsl::langandagents Ok(dsl::langandagents
.filter(dsl::language.eq(self.id)) .filter(dsl::language.eq(self.id))
.filter(dsl::relationship.eq(relationship)) .filter(dsl::relationship.eq(relationship))
.load::<LangAndAgent>(conn) .load::<LangAndAgent>(conn)
.map_err(|e| { .map_err(|e| {
DatabaseError::new( DatabaseError::new(
format!( format!("Failed to retrieve language relationship: {e:?}"),
"Failed to retrieve language relationship: {:?}",
e
),
"Database reading error", "Database reading error",
) )
})? })?
@ -94,19 +91,11 @@ impl Language {
.filter_map(|author| match author { .filter_map(|author| match author {
Ok(val) => Some(val), Ok(val) => Some(val),
Err(e) => { Err(e) => {
info!( info!("Failed ot retrieve author from database: {:?}", e);
"Failed ot retrieve author from database: {:?}",
e
);
None None
} }
}) })
.collect::<Vec<User>>()), .collect::<Vec<User>>())
Err(e) => {
panic!("Could not connect to the database: {:?}", e);
}
}
} }
} }

View File

@ -133,22 +133,19 @@ impl Word {
} }
#[graphql(description = "Language to which the word belongs")] #[graphql(description = "Language to which the word belongs")]
fn language(&self, context: &Context) -> Language { fn language(&self, context: &Context) -> FieldResult<Language> {
use schema::languages::dsl; use schema::languages::dsl;
match &mut context.db.conn() { use std::convert::Into;
Ok(conn) => { dsl::languages
match dsl::languages.find(self.language).first::<Language>(conn) .find(self.language)
{ .first::<Language>(&mut context.db.conn()?)
Ok(lang) => lang, .map_err(|e| DatabaseError::new(
Err(e) => { format!(
panic!("Failed to retrieve language {} of word {} from database: {:?}", "Failed to retrieve language {} of word {} from database: {e:?}",
self.language, self.norm, e self.language, self.norm
) ),
} "Database Error"
} ).into())
}
Err(e) => panic!("Failed to connect to database: {:?}", e),
}
} }
#[graphql( #[graphql(