Skip to content

Commit 121b96c

Browse files
committed
f Make returned errors more verbose
1 parent 2c2ae96 commit 121b96c

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/io/sqlite_store.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ impl KVStore for SqliteStore {
7878
},
7979
|row| row.get(0),
8080
)
81-
.map_err(|_| {
82-
let msg = format!("Failed to read from key: {}/{}", namespace, key);
83-
std::io::Error::new(std::io::ErrorKind::NotFound, msg)
81+
.map_err(|e| match e {
82+
rusqlite::Error::QueryReturnedNoRows => {
83+
let msg =
84+
format!("Failed to read as key could not be found: {}/{}", namespace, key);
85+
std::io::Error::new(std::io::ErrorKind::NotFound, msg)
86+
}
87+
e => {
88+
let msg = format!("Failed to read from key {}/{}: {}", namespace, key, e);
89+
std::io::Error::new(std::io::ErrorKind::Other, msg)
90+
}
8491
})?;
8592
Ok(Cursor::new(res))
8693
}
@@ -102,8 +109,8 @@ impl KVStore for SqliteStore {
102109
":value": buf,
103110
},
104111
)
105-
.map_err(|_| {
106-
let msg = format!("Failed to write to key: {}/{}", namespace, key);
112+
.map_err(|e| {
113+
let msg = format!("Failed to write to key {}/{}: {}", namespace, key, e);
107114
std::io::Error::new(std::io::ErrorKind::Other, msg)
108115
})?;
109116
Ok(())
@@ -122,8 +129,8 @@ impl KVStore for SqliteStore {
122129
":key": key,
123130
},
124131
)
125-
.map_err(|_| {
126-
let msg = format!("Failed to delete key: {}/{}", namespace, key);
132+
.map_err(|e| {
133+
let msg = format!("Failed to delete key {}/{}: {}", namespace, key, e);
127134
std::io::Error::new(std::io::ErrorKind::Other, msg)
128135
})?;
129136

@@ -136,21 +143,24 @@ impl KVStore for SqliteStore {
136143
let locked_conn = self.connection.lock().unwrap();
137144

138145
let sql = format!("SELECT key FROM {} WHERE namespace=:namespace", KV_TABLE_NAME);
139-
let mut stmt = locked_conn.prepare(&sql).map_err(|_| {
140-
std::io::Error::new(std::io::ErrorKind::Other, "Failed to prepare statement")
146+
let mut stmt = locked_conn.prepare(&sql).map_err(|e| {
147+
let msg = format!("Failed to prepare statement: {}", e);
148+
std::io::Error::new(std::io::ErrorKind::Other, msg)
141149
})?;
142150

143151
let mut keys = Vec::new();
144152

145153
let rows_iter = stmt
146154
.query_map(named_params! {":namespace": namespace, }, |row| row.get(0))
147-
.map_err(|_| {
148-
std::io::Error::new(std::io::ErrorKind::Other, "Failed to retrieve queried rows")
155+
.map_err(|e| {
156+
let msg = format!("Failed to retrieve queried rows: {}", e);
157+
std::io::Error::new(std::io::ErrorKind::Other, msg)
149158
})?;
150159

151160
for k in rows_iter {
152-
keys.push(k.map_err(|_| {
153-
std::io::Error::new(std::io::ErrorKind::Other, "Failed to retrieve queried rows")
161+
keys.push(k.map_err(|e| {
162+
let msg = format!("Failed to retrieve queried rows: {}", e);
163+
std::io::Error::new(std::io::ErrorKind::Other, msg)
154164
})?);
155165
}
156166

0 commit comments

Comments
 (0)