handle the error of the malformed time in database

This commit is contained in:
Abraham Toriz 2021-07-13 17:07:07 -05:00
parent 9d6a17b622
commit faef02d0a9
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 39 additions and 10 deletions

View File

@ -203,17 +203,30 @@ impl Database for SqliteDatabase {
fn entry_query(&self, query: &str, params: &[&dyn ToSql]) -> Result<Vec<Entry>> {
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<Vec<Meta>> {

View File

@ -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<T> = result::Result<T, Error>;