Skip to content

Commit bc35663

Browse files
committed
feat(sqlite): implement better error reporting and logging
1 parent a6a8fa4 commit bc35663

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/dictionary/sqlite.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
str,
66
};
77

8+
use log::debug;
89
use rusqlite::{Connection, Error as RusqliteError, OpenFlags, OptionalExtension, params};
910

1011
use super::{
@@ -50,11 +51,26 @@ pub enum SqliteDictionaryError {
5051

5152
impl Display for SqliteDictionaryError {
5253
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53-
write!(f, "sqlite error")
54+
match self {
55+
SqliteDictionaryError::SqliteError { source: _ } => {
56+
write!(f, "failed to perform sqlite operation")
57+
}
58+
SqliteDictionaryError::MissingTable { table } => {
59+
write!(f, "sqlite {table} does not exist")
60+
}
61+
SqliteDictionaryError::ReadOnly => write!(f, "sqlite file is readonly"),
62+
}
5463
}
5564
}
5665

57-
impl Error for SqliteDictionaryError {}
66+
impl Error for SqliteDictionaryError {
67+
fn source(&self) -> Option<&(dyn Error + 'static)> {
68+
match self {
69+
SqliteDictionaryError::SqliteError { source } => Some(source),
70+
_ => None,
71+
}
72+
}
73+
}
5874

5975
impl From<RusqliteError> for SqliteDictionaryError {
6076
fn from(value: RusqliteError) -> Self {
@@ -75,11 +91,16 @@ impl SqliteDictionary {
7591
/// TODO: doc
7692
pub fn open<P: AsRef<Path>>(path: P) -> Result<SqliteDictionary, SqliteDictionaryError> {
7793
let path = path.as_ref().to_path_buf();
94+
debug!("open sqlite dictionary at {path:?}");
7895
let mut conn = Connection::open(&path)?;
96+
debug!("initialize dictionary tables");
7997
Self::initialize_tables(&conn)?;
98+
debug!("migrate from userphrase_v1");
8099
Self::migrate_from_userphrase_v1(&mut conn)?;
100+
debug!("ensure tables exist");
81101
Self::ensure_tables(&conn)?;
82102
let info = Self::read_info_v1(&conn)?;
103+
debug!("read dictionary info {info:?}");
83104

84105
Ok(SqliteDictionary {
85106
conn,
@@ -179,16 +200,19 @@ impl SqliteDictionary {
179200
}
180201

181202
fn migrate_from_userphrase_v1(conn: &mut Connection) -> Result<(), SqliteDictionaryError> {
203+
debug!("query has_userphrase_v1");
182204
let has_userphrase_v1: bool = conn.query_row(
183205
"SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type='table' AND name='userphrase_v1')",
184206
[],
185207
|row| row.get(0)
186208
)?;
209+
debug!("query migrated");
187210
let migrated: bool = conn.query_row(
188211
"SELECT EXISTS (SELECT 1 FROM migration_v1 WHERE name='migrate_from_userphrase_v1')",
189212
[],
190213
|row| row.get(0),
191214
)?;
215+
debug!("has_userphrase_v1={has_userphrase_v1} migrated={migrated}");
192216
if !has_userphrase_v1 || migrated {
193217
// Don't need to migrate
194218
conn.execute(
@@ -233,13 +257,13 @@ impl SqliteDictionary {
233257
userphrases.push((
234258
syllables,
235259
row.get(0)?,
236-
row.get(1)?,
237-
row.get(2)?,
238-
row.get(3)?,
260+
row.get(1).unwrap_or(0),
261+
row.get(2).unwrap_or(0),
262+
row.get(3).unwrap_or(0),
239263
));
240264
}
241265
}
242-
266+
debug!("{} phrases loaded", userphrases.len());
243267
let tx = conn.transaction()?;
244268
{
245269
for item in userphrases {

0 commit comments

Comments
 (0)