summary refs log tree commit diff stats
path: root/src/db.rs
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-26 23:15:51 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-26 23:54:14 -0400
commit841e6a34a49261d2681c8a4d2c1dc176ba4e3b35 (patch)
tree726d6f77657ea79740a32c0ae55db88242591172 /src/db.rs
parent8ecc2943a4241d466a9bf5a359260a958e3d3b46 (diff)
downloadclinte-841e6a34a49261d2681c8a4d2c1dc176ba4e3b35.tar.gz
removed panics and refactored error handling
Using a helper function to handle fatal errors
  error::helper()
Displays the simplified message if an error condition
occurs. Displays both the simplified and the raw error
message if -v verbose logging is enabled.
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs29
1 files changed, 17 insertions, 12 deletions
diff --git a/src/db.rs b/src/db.rs
index 935f1f9..84f209b 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,6 +1,7 @@
 use std::time;
 
 use crate::conf;
+use crate::error;
 
 const DB_PATH: &str = "/usr/local/clinte/clinte.db";
 
@@ -25,24 +26,28 @@ impl Conn {
             log::info!("Connecting to database");
         }
 
-        let conn = rusqlite::Connection::open_with_flags(
-            path,
-            rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX
-                | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
-                | rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE,
-        )
-        .expect("Could not connect to DB");
+        let conn = error::helper(
+            rusqlite::Connection::open_with_flags(
+                path,
+                rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX
+                    | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
+                    | rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE,
+            ),
+            "Could not connect to DB",
+        );
 
-        conn.execute(
-            "CREATE TABLE IF NOT EXISTS posts (
+        error::helper(
+            conn.execute(
+                "CREATE TABLE IF NOT EXISTS posts (
             id INTEGER PRIMARY KEY NOT NULL,
             title TEXT NOT NULL,
             author TEXT NOT NULL,
             body TEXT NOT NULL
         )",
-            rusqlite::NO_PARAMS,
-        )
-        .expect("Could not initialize DB");
+                rusqlite::NO_PARAMS,
+            ),
+            "Could not initialize DB",
+        );
 
         if *conf::DEBUG {
             log::info!(