Skip to content

Commit a4428df

Browse files
committed
f Make constructor fallible
1 parent 9413863 commit a4428df

File tree

1 file changed

+22
-14
lines changed
  • lightning-persister/src/sqlite_store

1 file changed

+22
-14
lines changed

lightning-persister/src/sqlite_store/mod.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,42 @@ impl SqliteStore {
3939
let db_file_name = db_file_name.unwrap_or(DEFAULT_SQLITE_DB_FILE_NAME.to_string());
4040
let kv_table_name = kv_table_name.unwrap_or(DEFAULT_KV_TABLE_NAME.to_string());
4141

42-
fs::create_dir_all(data_dir.clone()).unwrap_or_else(|_| {
43-
panic!("Failed to create database destination directory: {}", data_dir.display())
44-
});
42+
fs::create_dir_all(data_dir.clone()).map_err(|e| {
43+
let msg = format!("Failed to create database destination directory: {}",
44+
data_dir.display());
45+
io::Error::new(e.kind(), msg)
46+
})?;
4547
let mut db_file_path = data_dir.clone();
4648
db_file_path.push(db_file_name);
4749

48-
let connection = Connection::open(db_file_path.clone()).unwrap_or_else(|_| {
49-
panic!("Failed to open/create database file: {}", db_file_path.display())
50-
});
50+
let connection = Connection::open(db_file_path.clone()).map_err(|e| {
51+
let msg = format!("Failed to open/create database file: {}",
52+
db_file_path.display());
53+
io::Error::new(e.kind(), msg)
54+
})?;
5155

52-
connection
53-
.pragma(Some(rusqlite::DatabaseName::Main), "user_version", SCHEMA_USER_VERSION, |_| {
56+
connection.pragma(Some(rusqlite::DatabaseName::Main),
57+
"user_version", SCHEMA_USER_VERSION, |_| {
5458
Ok(())
55-
})
56-
.unwrap_or_else(|_| panic!("Failed to set PRAGMA user_version"));
59+
}).map_err(|e| {
60+
let msg = format!("Failed to set PRAGMA user_version");
61+
io::Error::new(e.kind(), msg)
62+
})?;
5763

5864
let sql = format!(
5965
"CREATE TABLE IF NOT EXISTS {} (
6066
namespace TEXT NOT NULL,
6167
sub_namespace TEXT DEFAULT \"\" NOT NULL,
6268
key TEXT NOT NULL CHECK (key <> ''),
6369
value BLOB, PRIMARY KEY ( namespace, sub_namespace, key )
64-
);",
70+
);",
6571
kv_table_name
6672
);
67-
connection
68-
.execute(&sql, [])
69-
.unwrap_or_else(|_| panic!("Failed to create table: {}", kv_table_name));
73+
74+
connection.execute(&sql, []).map_err(|e| {
75+
let msg = format!("Failed to create table: {}", kv_table_name);
76+
io::Error::new(e.kind(), msg)
77+
})?;
7078

7179
let connection = Arc::new(Mutex::new(connection));
7280
Self { connection, data_dir, kv_table_name }

0 commit comments

Comments
 (0)