feat: 🎸 Handle corrupted sqlite DB (#2976)

* feat: 🎸 Handle corrupted sqlite DB
This commit is contained in:
Zhihe Li committed 2025-09-03 11:41:42 -04:00
1 parent cb8b21c10c
commit dc026c44a1
2 files changed
+14 -4

No files matched your search

+2 -2
View File
@@ -182,8 +182,8 @@ export default class SqliteHandler {
// If we get an invalid list token, we'll delete the token and
// clear the DB and try again without a token.
if (
err?.errors[0].status === 400 &&
err?.errors[0].code === 'invalid list token'
err?.errors?.[0].status === 400 &&
err?.errors?.[0].code === 'invalid list token'
) {
await this.sqlite.deleteResource(type);
// Delete all tokens of the same resource type so we don't keep clearing the DB.
+12 -2
View File
@@ -27,6 +27,10 @@ const MAX_HOST_PARAMETERS = 32766;
const SCHEMA_VERSION = 1;
const isSecure = self.isSecureContext;
// Error code for sqlite corruption errors.
// See https://sqlite.org/rescode.html#corrupt
const SQLITE_CORRUPT = 11;
const methods = {
initializeSQLite: async () => {
sqlite3 = await sqlite3InitModule({
@@ -65,7 +69,7 @@ const methods = {
setupDb();
done = true;
} catch (err) {
} catch (/** @type {import('@sqlite.org/sqlite-wasm').SQLite3Error} */ err) {
// If we get a SQLite3Error and the pool is at capacity, we'll assume the error is
// because we hit the cap so we add more capacity and see if that solves it.
if (
@@ -81,7 +85,13 @@ const methods = {
}
} else {
console.error('SQLite initialization error:', err);
throw err;
// We might have gotten ourselves into a bad state which corrupted the DB so clear it out and retry
if (err?.resultCode === SQLITE_CORRUPT) {
poolUtil.unlink(`/${dbName}`);
} else {
throw err;
}
}
}
}