Skip to content

Commit 4ed0e13

Browse files
committed
op_store: propagate initialization error
1 parent caaa2ab commit 4ed0e13

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/src/repo.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ pub enum RepoInitError {
157157

158158
impl ReadonlyRepo {
159159
pub fn default_op_store_initializer() -> &'static OpStoreInitializer<'static> {
160-
&|_settings, store_path, root_data| Ok(Box::new(SimpleOpStore::init(store_path, root_data)))
160+
&|_settings, store_path, root_data| {
161+
Ok(Box::new(SimpleOpStore::init(store_path, root_data)?))
162+
}
161163
}
162164

163165
pub fn default_op_heads_store_initializer() -> &'static OpHeadsStoreInitializer<'static> {

lib/src/simple_op_store.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use prost::Message;
3232
use tempfile::NamedTempFile;
3333
use thiserror::Error;
3434

35+
use crate::backend::BackendInitError;
3536
use crate::backend::CommitId;
3637
use crate::backend::MillisSinceEpoch;
3738
use crate::backend::Timestamp;
@@ -64,6 +65,17 @@ use crate::op_store::WorkspaceId;
6465
const OPERATION_ID_LENGTH: usize = 64;
6566
const VIEW_ID_LENGTH: usize = 64;
6667

68+
/// Error that may occur during [`SimpleOpStore`] initialization.
69+
#[derive(Debug, Error)]
70+
#[error("Failed to initialize simple operation store")]
71+
pub struct SimpleOpStoreInitError(#[from] pub PathError);
72+
73+
impl From<SimpleOpStoreInitError> for BackendInitError {
74+
fn from(err: SimpleOpStoreInitError) -> Self {
75+
BackendInitError(err.into())
76+
}
77+
}
78+
6779
#[derive(Debug, Error)]
6880
#[error("Failed to read {kind} with ID {id}")]
6981
struct DecodeError {
@@ -92,11 +104,14 @@ impl SimpleOpStore {
92104
"simple_op_store"
93105
}
94106

95-
/// Creates an empty OpStore, panics if it already exists
96-
pub fn init(store_path: &Path, root_data: RootOperationData) -> Self {
107+
/// Creates an empty OpStore. Returns error if it already exists.
108+
pub fn init(
109+
store_path: &Path,
110+
root_data: RootOperationData,
111+
) -> Result<Self, SimpleOpStoreInitError> {
97112
let store = Self::new(store_path, root_data);
98-
store.init_base_dirs().unwrap(); // TODO: propagate error
99-
store
113+
store.init_base_dirs()?;
114+
Ok(store)
100115
}
101116

102117
/// Load an existing OpStore
@@ -794,7 +809,7 @@ mod tests {
794809
let root_data = RootOperationData {
795810
root_commit_id: CommitId::from_hex("000000"),
796811
};
797-
let store = SimpleOpStore::init(temp_dir.path(), root_data);
812+
let store = SimpleOpStore::init(temp_dir.path(), root_data).unwrap();
798813
let view = create_view();
799814
let view_id = store.write_view(&view).unwrap();
800815
let read_view = store.read_view(&view_id).unwrap();
@@ -807,7 +822,7 @@ mod tests {
807822
let root_data = RootOperationData {
808823
root_commit_id: CommitId::from_hex("000000"),
809824
};
810-
let store = SimpleOpStore::init(temp_dir.path(), root_data);
825+
let store = SimpleOpStore::init(temp_dir.path(), root_data).unwrap();
811826
let operation = create_operation();
812827
let op_id = store.write_operation(&operation).unwrap();
813828
let read_operation = store.read_operation(&op_id).unwrap();

0 commit comments

Comments
 (0)