diff --git a/src/error.rs b/src/error.rs index 8fa5cb1ce..db15e3c42 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,6 +9,7 @@ use schema; use serde_json; use std::path::PathBuf; use std::sync::PoisonError; +use core::LOCKFILE_FILEPATH; /// The library's failure based error enum #[derive(Debug, Fail)] @@ -19,6 +20,9 @@ pub enum TantivyError { /// File already exists, this is a problem when we try to write into a new file. #[fail(display = "file already exists: '{:?}'", _0)] FileAlreadyExists(PathBuf), + /// Lockfile already exists + #[fail(display = "Lockfile '{:?}' already exists. Possible causes: another IndexWriter instance or panic during previous lock drop.", _0)] + LockFileAlreadyExists(PathBuf), /// IO Error. #[fail(display = "an IO error occurred: '{}'", _0)] IOError(#[cause] IOError), @@ -95,7 +99,12 @@ impl From for TantivyError { fn from(error: OpenWriteError) -> TantivyError { match error { OpenWriteError::FileAlreadyExists(filepath) => { - TantivyError::FileAlreadyExists(filepath) + let lockfile_fname = LOCKFILE_FILEPATH.to_str().unwrap(); + if filepath.ends_with(lockfile_fname) { + TantivyError::LockFileAlreadyExists(filepath) + } else { + TantivyError::FileAlreadyExists(filepath) + } } OpenWriteError::IOError(io_error) => TantivyError::IOError(io_error), }.into() diff --git a/src/indexer/index_writer.rs b/src/indexer/index_writer.rs index 982140fbc..226fb7379 100644 --- a/src/indexer/index_writer.rs +++ b/src/indexer/index_writer.rs @@ -657,11 +657,27 @@ mod tests { let index = Index::create_in_ram(schema_builder.build()); let _index_writer = index.writer(40_000_000).unwrap(); match index.writer(40_000_000) { - Err(TantivyError::FileAlreadyExists(_)) => {} + Err(TantivyError::LockFileAlreadyExists(_)) => {} _ => panic!("Expected FileAlreadyExists error"), } } + #[test] + fn test_lockfile_already_exists_error_msg() { + let schema_builder = schema::SchemaBuilder::default(); + let index = Index::create_in_ram(schema_builder.build()); + let _index_writer = index.writer_with_num_threads(1, 3_000_000).unwrap(); + match index.writer_with_num_threads(1, 3_000_000) { + Err(err) => { + let err_msg = err.to_string(); + assert!(err_msg.contains("Lockfile")); + assert!(err_msg.contains("already exists")); + assert!(err_msg.contains("Possible causes:")) + }, + _ => panic!("Expected LockfileAlreadyExists error"), + } + } + #[test] fn test_set_merge_policy() { let schema_builder = schema::SchemaBuilder::default();