@@ -42,3 +42,53 @@ const SQLITE3MC_DECRYPT_ERROR_PATTERNS: readonly RegExp[] = [
4242export function isDecryptFailureMessage ( message : string ) : boolean {
4343 return SQLITE3MC_DECRYPT_ERROR_PATTERNS . some ( p => p . test ( message ) ) ;
4444}
45+
46+ /**
47+ * Error thrown by sqlite-opfs when the on-disk database image is corrupt
48+ * (`SQLITE_CORRUPT`, result code 11 — "database disk image is malformed").
49+ *
50+ * Distinct from {@link SqliteEncryptionError}: no key recovers a corrupt image,
51+ * so there is nothing to retry. The only escape is to delete the store and start
52+ * fresh, so consumers pattern-match on `instanceof SqliteCorruptionError` to wipe
53+ * and reopen rather than surfacing a dead-end error.
54+ **/
55+ export class SqliteCorruptionError extends Error {
56+ constructor ( message : string , opts ?: { cause ?: unknown } ) {
57+ super ( message , opts ?. cause !== undefined ? { cause : opts . cause } : undefined ) ;
58+ this . name = 'SqliteCorruptionError' ;
59+ }
60+ }
61+
62+ /** Error thrown when another browser context already owns a store's OPFS pool. */
63+ export class SqlitePoolBusyError extends Error {
64+ constructor ( public readonly poolDirectory : string ) {
65+ super ( `SQLite-OPFS pool "${ poolDirectory } " is already in use by another store instance` ) ;
66+ this . name = 'SqlitePoolBusyError' ;
67+ }
68+ }
69+
70+ /** Error thrown when the browser context does not expose the Web Locks API required by the OPFS SAH pool. */
71+ export class SqliteWebLocksUnavailableError extends Error {
72+ constructor ( ) {
73+ super ( 'SQLite-OPFS requires the Web Locks API, but it is unavailable in this browser context' ) ;
74+ this . name = 'SqliteWebLocksUnavailableError' ;
75+ }
76+ }
77+
78+ /**
79+ * Strings/codes raised by SQLite when a database image is corrupt. Kept disjoint
80+ * from {@link SQLITE3MC_DECRYPT_ERROR_PATTERNS}: "file is not a database"
81+ * (SQLITE_NOTADB) is the decrypt signal, whereas a malformed image is the
82+ * genuinely-unrecoverable SQLITE_CORRUPT.
83+ **/
84+ const SQLITE_CORRUPTION_ERROR_PATTERNS : readonly RegExp [ ] = [
85+ / d a t a b a s e d i s k i m a g e i s m a l f o r m e d / i,
86+ / \b S Q L I T E _ C O R R U P T \b / i,
87+ ] ;
88+
89+ /**
90+ * Returns `true` if `message` matches one of the known SQLite corruption strings.
91+ **/
92+ export function isCorruptionMessage ( message : string ) : boolean {
93+ return SQLITE_CORRUPTION_ERROR_PATTERNS . some ( p => p . test ( message ) ) ;
94+ }
0 commit comments