diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-11-07 19:49:21 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-11-07 20:07:14 -0500 |
commit | b18c6b8135bfdc884b0a0cf03196d3de0a343490 (patch) | |
tree | 4c3c1e758e73e51557492af22d1e2d774f5b30d2 /src/db.rs | |
parent | 1e1a47061c455a5324d8a62fa9bcee9ecdf9c4cb (diff) | |
download | lastfm-query-b18c6b8135bfdc884b0a0cf03196d3de0a343490.tar.gz lastfm-query-b18c6b8135bfdc884b0a0cf03196d3de0a343490.zip |
better top level error handling
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 23 |
1 files changed, 20 insertions, 3 deletions
@@ -1,6 +1,8 @@ use error::Result; use lastfm; +use failure::Fail; + const SCHEMA: &'static str = " CREATE TABLE `tracks` ( artist varchar(1024) NOT NULL, @@ -17,7 +19,14 @@ pub struct DB { impl DB { pub fn new<P: AsRef<std::path::Path>>(path: &P) -> Result<DB> { let conn = if path.as_ref().exists() { - rusqlite::Connection::open(path)? + rusqlite::Connection::open(path) + .map_err(|e| { + let msg = format!( + "couldn't open db at {}", + path.as_ref().display() + ); + e.context(msg) + })? } else { Self::create(path)? @@ -36,8 +45,16 @@ impl DB { if let Some(parent) = path.as_ref().parent() { std::fs::create_dir_all(parent)?; - let conn = rusqlite::Connection::open(path)?; - conn.execute(SCHEMA, rusqlite::NO_PARAMS)?; + let conn = rusqlite::Connection::open(path) + .map_err(|e| { + let msg = format!( + "couldn't create db at {}", + path.as_ref().display() + ); + e.context(msg) + })?; + conn.execute(SCHEMA, rusqlite::NO_PARAMS) + .map_err(|e| e.context("failed to execute schema"))?; Ok(conn) } else { |