From 6fef04be20021019eb5d01691ab7aa9a68f3a2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Mon, 9 Dec 2019 16:54:44 +0100 Subject: [PATCH] Remove unsound unwraps from the synonym routes --- meilisearch-http/src/routes/synonym.rs | 29 +++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/meilisearch-http/src/routes/synonym.rs b/meilisearch-http/src/routes/synonym.rs index 31222389a..5affed209 100644 --- a/meilisearch-http/src/routes/synonym.rs +++ b/meilisearch-http/src/routes/synonym.rs @@ -40,7 +40,7 @@ pub async fn list(ctx: Context) -> SResult { .map_err(ResponseError::internal)?; let synonyms_fst = synonyms_fst.unwrap_or_default(); - let synonyms_list = synonyms_fst.stream().into_strs().unwrap(); + let synonyms_list = synonyms_fst.stream().into_strs().map_err(ResponseError::internal)?; let mut response = HashMap::new(); @@ -49,12 +49,12 @@ pub async fn list(ctx: Context) -> SResult { for synonym in synonyms_list { let alternative_list = index_synonyms .synonyms(&reader, synonym.as_bytes()) - .unwrap() - .unwrap() - .stream() - .into_strs() - .unwrap(); - response.insert(synonym, alternative_list); + .map_err(ResponseError::internal)?; + + if let Some(list) = alternative_list { + let list = list.stream().into_strs().map_err(ResponseError::internal)?; + response.insert(synonym, list); + } } Ok(tide::response::json(response)) @@ -71,13 +71,14 @@ pub async fn get(ctx: Context) -> SResult { let synonym_list = index .synonyms .synonyms(&reader, synonym.as_bytes()) - .unwrap() - .unwrap() - .stream() - .into_strs() - .unwrap(); + .map_err(ResponseError::internal)?; - Ok(tide::response::json(synonym_list)) + let list = match synonym_list { + Some(list) => list.stream().into_strs().map_err(ResponseError::internal)?, + None => Vec::new(), + }; + + Ok(tide::response::json(list)) } pub async fn create(mut ctx: Context) -> SResult { @@ -217,7 +218,7 @@ pub async fn clear(ctx: Context) -> SResult { .map_err(ResponseError::internal)?; let synonyms_fst = synonyms_fst.unwrap_or_default(); - let synonyms_list = synonyms_fst.stream().into_strs().unwrap(); + let synonyms_list = synonyms_fst.stream().into_strs().map_err(ResponseError::internal)?; let mut synonyms_deletion = index.synonyms_deletion(); for synonym in synonyms_list {