Avoid panics (#392)

- Use `Result` as much as possible
- Display errors instead of panicking

TODO (maybe in another PR? this one is already quite big):
- Find a way to merge Ructe/ErrorPage types, so that we can have routes returning `Result<X, ErrorPage>` instead of panicking when we have an `Error`
- Display more details about the error, to make it easier to debug

(sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)
This commit is contained in:
Baptiste Gelez
2018-12-29 09:36:07 +01:00
committed by GitHub
parent 4059a840be
commit 80a4dae8bd
50 changed files with 1879 additions and 1990 deletions
+5 -5
View File
@@ -118,7 +118,7 @@ pub(crate) mod tests {
conn.test_transaction::<_, (), _>(|| {
let searcher = get_searcher();
let blog = &fill_database(conn).1[0];
let author = &blog.list_authors(conn)[0];
let author = &blog.list_authors(conn).unwrap()[0];
let title = random_hex()[..8].to_owned();
@@ -134,23 +134,23 @@ pub(crate) mod tests {
subtitle: "".to_owned(),
source: "".to_owned(),
cover_id: None,
}, &searcher);
}, &searcher).unwrap();
PostAuthor::insert(conn, NewPostAuthor {
post_id: post.id,
author_id: author.id,
});
}).unwrap();
searcher.commit();
assert_eq!(searcher.search_document(conn, Query::from_str(&title), (0,1))[0].id, post.id);
let newtitle = random_hex()[..8].to_owned();
post.title = newtitle.clone();
post.update(conn, &searcher);
post.update(conn, &searcher).unwrap();
searcher.commit();
assert_eq!(searcher.search_document(conn, Query::from_str(&newtitle), (0,1))[0].id, post.id);
assert!(searcher.search_document(conn, Query::from_str(&title), (0,1)).is_empty());
post.delete(&(conn, &searcher));
post.delete(&(conn, &searcher)).unwrap();
searcher.commit();
assert!(searcher.search_document(conn, Query::from_str(&newtitle), (0,1)).is_empty());
+23 -21
View File
@@ -14,9 +14,10 @@ use std::{cmp, fs::create_dir_all, path::Path, sync::Mutex};
use search::query::PlumeQuery;
use super::tokenizer;
use Result;
#[derive(Debug)]
pub enum SearcherError{
pub enum SearcherError {
IndexCreationError,
WriteLockAcquisitionError,
IndexOpeningError,
@@ -66,7 +67,7 @@ impl Searcher {
}
pub fn create(path: &AsRef<Path>) -> Result<Self,SearcherError> {
pub fn create(path: &AsRef<Path>) -> Result<Self> {
let whitespace_tokenizer = tokenizer::WhitespaceTokenizer
.filter(LowerCaser);
@@ -94,7 +95,7 @@ impl Searcher {
})
}
pub fn open(path: &AsRef<Path>) -> Result<Self, SearcherError> {
pub fn open(path: &AsRef<Path>) -> Result<Self> {
let whitespace_tokenizer = tokenizer::WhitespaceTokenizer
.filter(LowerCaser);
@@ -121,7 +122,7 @@ impl Searcher {
})
}
pub fn add_document(&self, conn: &Connection, post: &Post) {
pub fn add_document(&self, conn: &Connection, post: &Post) -> Result<()> {
let schema = self.index.schema();
let post_id = schema.get_field("post_id").unwrap();
@@ -142,18 +143,19 @@ impl Searcher {
let mut writer = self.writer.lock().unwrap();
let writer = writer.as_mut().unwrap();
writer.add_document(doc!(
post_id => i64::from(post.id),
author => post.get_authors(conn).into_iter().map(|u| u.get_fqn(conn)).join(" "),
creation_date => i64::from(post.creation_date.num_days_from_ce()),
instance => Instance::get(conn, post.get_blog(conn).instance_id).unwrap().public_domain.clone(),
tag => Tag::for_post(conn, post.id).into_iter().map(|t| t.tag).join(" "),
blog_name => post.get_blog(conn).title,
content => post.content.get().clone(),
subtitle => post.subtitle.clone(),
title => post.title.clone(),
lang => detect_lang(post.content.get()).and_then(|i| if i.is_reliable() { Some(i.lang()) } else {None} ).unwrap_or(Lang::Eng).name(),
license => post.license.clone(),
));
post_id => i64::from(post.id),
author => post.get_authors(conn)?.into_iter().map(|u| u.get_fqn(conn)).join(" "),
creation_date => i64::from(post.creation_date.num_days_from_ce()),
instance => Instance::get(conn, post.get_blog(conn)?.instance_id)?.public_domain.clone(),
tag => Tag::for_post(conn, post.id)?.into_iter().map(|t| t.tag).join(" "),
blog_name => post.get_blog(conn)?.title,
content => post.content.get().clone(),
subtitle => post.subtitle.clone(),
title => post.title.clone(),
lang => detect_lang(post.content.get()).and_then(|i| if i.is_reliable() { Some(i.lang()) } else {None} ).unwrap_or(Lang::Eng).name(),
license => post.license.clone(),
));
Ok(())
}
pub fn delete_document(&self, post: &Post) {
@@ -166,9 +168,9 @@ impl Searcher {
writer.delete_term(doc_id);
}
pub fn update_document(&self, conn: &Connection, post: &Post) {
pub fn update_document(&self, conn: &Connection, post: &Post) -> Result<()> {
self.delete_document(post);
self.add_document(conn, post);
self.add_document(conn, post)
}
pub fn search_document(&self, conn: &Connection, query: PlumeQuery, (min, max): (i32, i32)) -> Vec<Post>{
@@ -185,9 +187,9 @@ impl Searcher {
.filter_map(|doc_add| {
let doc = searcher.doc(*doc_add).ok()?;
let id = doc.get_first(post_id)?;
Post::get(conn, id.i64_value() as i32)
//borrow checker don't want me to use filter_map or and_then here
})
Post::get(conn, id.i64_value() as i32).ok()
//borrow checker don't want me to use filter_map or and_then here
})
.collect()
}