commit 7ff982331e416f9f29de900c1a5bfcd561b92fed from: witcher date: Sun Nov 20 17:17:57 2022 UTC Fix panic on insertion and invalid time stamp rss-email would panic when inserting a post that already is inserted (primary key already exists), so the insertion is skipped entirely (`INSERT OR IGNORE`). If a post has an invalid time stamp, the application would also panic, even though the timestamp is not crucial data. Instead, a default is used for the timestamp. commit - 0468e48dcfca8923ccf77f08584567b4f3730553 commit + 7ff982331e416f9f29de900c1a5bfcd561b92fed blob - 2c2336a684b75dc631aafa03a7ed6bdfac663997 blob + ab34aff9e612ca78b39aab7c6084f2b7b1e2778e --- Cargo.lock +++ Cargo.lock @@ -1138,7 +1138,7 @@ dependencies = [ [[package]] name = "rss-email" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "chrono", blob - ad8c2c8efada1900e403700941516b271040e5b7 blob + d217fc506c8db494b1eae9f74b48c4fc1969dd1e --- src/db.rs +++ src/db.rs @@ -5,9 +5,11 @@ use sqlx::Sqlite; // inserts a new post or updates an old one with the same guid pub async fn insert_item(mut conn: PoolConnection, item: &Item) -> anyhow::Result<()> { - let time = item - .pub_date() - .map(|date| DateTime::parse_from_rfc2822(date).unwrap().timestamp()); + let time = item.pub_date().map(|date| { + DateTime::parse_from_rfc2822(date) + .unwrap_or_else(|_| DateTime::default()) + .timestamp() + }); let guid = item.guid().ok_or_else(|| anyhow!("No guid found"))?.value(); let title = item.title(); @@ -17,7 +19,7 @@ pub async fn insert_item(mut conn: PoolConnection anyhow::Result<()> { for i in items { let conn = pool.acquire().await?; - db::insert_item(conn, i).await?; + db::insert_item(conn, i).await.context(format!( + "Unable to insert item from {:?} with GUID {:?}", + i.link(), + i.guid() + ))?; } } blob - af377d139f71bc562e75be4281aecfad59e9702f blob + 3141cb80595ac1326c51b00c0d95e9814ada7654 --- src/rss.rs +++ src/rss.rs @@ -1,11 +1,14 @@ use reqwest; use rss; +use crate::anyhow::Context; + pub async fn fetch_new>(url: S) -> anyhow::Result { debug!("Fetching feed for {}", url.as_ref()); let content = reqwest::get(url.as_ref()).await?.bytes().await?; - let channel = rss::Channel::read_from(&content[..])?; + let channel = rss::Channel::read_from(&content[..]) + .context(format!("Unable to read from RSS feed {}", url.as_ref()))?; Ok(channel) }