From 57a8b5091f3813ac24d8f1f945094d9afbe98592 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 14 Nov 2024 14:06:03 +0200 Subject: [PATCH] fix db errors handle --- src/profile/bookmark.rs | 11 ++++++++--- src/profile/bookmark/database.rs | 10 +++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index 222ccd75..59c4a3b5 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -25,10 +25,15 @@ impl Bookmark { let memory = Rc::new(Memory::new()); // Build initial index - for record in database.records(None) { - if memory.add(record.request, record.id).is_err() { - todo!() + match database.records(None) { + Ok(records) => { + for record in records { + if memory.add(record.request, record.id).is_err() { + todo!() + } + } } + Err(reason) => todo!("{reason}"), } // Return new `Self` diff --git a/src/profile/bookmark/database.rs b/src/profile/bookmark/database.rs index 0a4866c4..a361420c 100644 --- a/src/profile/bookmark/database.rs +++ b/src/profile/bookmark/database.rs @@ -28,10 +28,10 @@ impl Database { // Getters /// Get bookmark records from database with optional filter by `request` - pub fn records(&self, request: Option<&str>) -> Vec { + pub fn records(&self, request: Option<&str>) -> Result, Error> { let readable = self.connection.read().unwrap(); - let tx = readable.unchecked_transaction().unwrap(); - select(&tx, *self.profile_id, request).unwrap() + let tx = readable.unchecked_transaction()?; + select(&tx, *self.profile_id, request) } // Setters @@ -41,10 +41,10 @@ impl Database { pub fn add(&self, time: DateTime, request: String) -> Result { // Begin new transaction let mut writable = self.connection.write().unwrap(); - let tx = writable.transaction().unwrap(); + let tx = writable.transaction()?; // Create new record - insert(&tx, *self.profile_id, time, request).unwrap(); + insert(&tx, *self.profile_id, time, request)?; // Hold insert ID for result let id = last_insert_id(&tx);