From faef02d0a91531fbed227c2e3f6986a963e6d3a0 Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Tue, 13 Jul 2021 17:07:07 -0500 Subject: [PATCH] handle the error of the malformed time in database --- src/database.rs | 33 +++++++++++++++++++++++---------- src/error.rs | 16 ++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/database.rs b/src/database.rs index bd5546c..05346cc 100644 --- a/src/database.rs +++ b/src/database.rs @@ -203,17 +203,30 @@ impl Database for SqliteDatabase { fn entry_query(&self, query: &str, params: &[&dyn ToSql]) -> Result> { let mut stmt = self.connection.prepare(query)?; - let results = stmt.query_map(params, |row| { - Ok(Entry { - id: row.get("id")?, - note: row.get("note")?, - start: row.get("start")?, - end: row.get("end")?, - sheet: row.get("sheet")?, - }) - })?.filter_map(|r| r.ok()).collect(); + let x = stmt.query_and_then(params, |row| { + let id: u64 = row.get("id")?; + let note = row.get("note")?; + let sheet = row.get("sheet")?; - Ok(results) + let start = row.get("start").map_err(|_| { + Error::InvalidTimeInDatabase { + id: id.to_string(), + col: "start".into(), + } + })?; + let end = row.get("start").map_err(|_| { + Error::InvalidTimeInDatabase { + id: id.to_string(), + col: "start".into(), + } + })?; + + Ok(Entry { + id, note, start, end, sheet, + }) + })?.collect(); + + x } fn meta_query(&self, query: &str, params: &[&dyn ToSql]) -> Result> { diff --git a/src/error.rs b/src/error.rs index e667e5e..e381da1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -54,6 +54,22 @@ pub enum Error { #[error("Could not understand '{0}' as a month specifier. Try 'this', 'last', or any month name like 'january' or 'nov'")] InvalidMonthSpec(String), + + #[error("An error ocurred while trying to read entries from the database. + +In the row with id {id} the data at column '{col}' has a value that is not a +valid time. + +A valid time looks like this: '2021-07-12 19:35:43.645916' and must be in UTC +timezone (unless you are using a database that was created by timetrap, case in +which the times are in the local timezone). + +To fix this problem you can use t backend and manually update the value using +good old SQL.")] + InvalidTimeInDatabase { + id: String, + col: String, + }, } pub type Result = result::Result;