From a9236e38bd6eb6c27df25a077be7ee7bdf1f47ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Thu, 4 Jul 2024 13:46:17 +0200 Subject: [PATCH] Improve safety of cache entry It seems a comment can have a lower timestamp than the submission? This is strange because it would point to inconsistencies in the storage algorithm. In any case, considering this could also be a local artifact, it is important to just have that part of the re-creation algorithm of the cache fail while the rest continues to run. --- src/cache.mjs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/cache.mjs b/src/cache.mjs index 22689f9f..4be31f94 100644 --- a/src/cache.mjs +++ b/src/cache.mjs @@ -8,6 +8,8 @@ import Database from "better-sqlite3"; import { add } from "date-fns"; import normalizeUrl from "normalize-url"; +import log from "./logger.mjs"; + const dbPath = join(process.env.CACHE_DIR, "database.db"); const db = new Database(dbPath); db.pragma("journal_mode = WAL"); @@ -556,14 +558,18 @@ function insertMessage(message) { const insertComment = db.prepare( `INSERT INTO comments (id, submission_id, timestamp, title, signer, identity) VALUES (?, ?, ?, ?, ?, ?)`, ); - insertComment.run( - `kiwi:0x${index}`, - href, - timestamp, - title, - signer, - identity, - ); + try { + insertComment.run( + `kiwi:0x${index}`, + href, + timestamp, + title, + signer, + identity, + ); + } catch (err) { + log(`Failing to insert comment "${title}", error: "${err.toString()}"`); + } } else { throw new Error("Unsupported message type"); }