|
1 | 1 | const NITRO_SQLITE_ERROR_NAME = 'NitroSQLiteError' as const |
2 | 2 | const NATIVE_EXCEPTION_PREFIX = '[NativeNitroSQLiteException][' |
3 | 3 |
|
4 | | -/** Category attached to an error thrown by NitroSQLite's native implementation. */ |
| 4 | +/** |
| 5 | + * Native error categories exposed by {@linkcode NitroSQLiteError.type}. |
| 6 | + * Compare against these constants when handling a specific failure. The values |
| 7 | + * are categories, not SQLite's numeric result codes. Import them from |
| 8 | + * `react-native-nitro-sqlite/errors` to avoid initializing the native module. |
| 9 | + * @see {@linkcode NitroSQLiteError} |
| 10 | + */ |
| 11 | +export const NitroSQLiteExceptionType = { |
| 12 | + /** Native failure without a more specific category. */ |
| 13 | + UnknownError: 'UnknownError', |
| 14 | + /** Open failure, including invalid paths and an already-open default connection. */ |
| 15 | + DatabaseCannotBeOpened: 'DatabaseCannotBeOpened', |
| 16 | + /** An encryption key was supplied to a build without SQLite SEE. */ |
| 17 | + EncryptionNotEnabled: 'EncryptionNotEnabled', |
| 18 | + /** Invalid encryption key, unreadable encrypted database, or existing plaintext file. */ |
| 19 | + DatabaseCannotBeDecrypted: 'DatabaseCannotBeDecrypted', |
| 20 | + /** Category for a database connection that is not open. */ |
| 21 | + DatabaseNotOpen: 'DatabaseNotOpen', |
| 22 | + /** Database attachment failure or an unopened native connection. */ |
| 23 | + UnableToAttachToDatabase: 'UnableToAttachToDatabase', |
| 24 | + /** SQL execution or parameter binding failure, including a missing database file. */ |
| 25 | + SqlExecutionError: 'SqlExecutionError', |
| 26 | + /** A SQL file could not be read or executed. */ |
| 27 | + CouldNotLoadFile: 'CouldNotLoadFile', |
| 28 | + /** An empty batch was submitted. */ |
| 29 | + NoBatchCommandsProvided: 'NoBatchCommandsProvided', |
| 30 | +} as const |
| 31 | + |
| 32 | +/** |
| 33 | + * Category attached to {@linkcode NitroSQLiteError.type} for a recognized native |
| 34 | + * failure. The corresponding {@linkcode NitroSQLiteExceptionType} object exposes |
| 35 | + * the same values at runtime. |
| 36 | + * @see {@linkcode NitroSQLiteError.type} |
| 37 | + */ |
5 | 38 | export type NitroSQLiteExceptionType = |
6 | | - | 'UnknownError' |
7 | | - | 'DatabaseCannotBeOpened' |
8 | | - | 'EncryptionNotEnabled' |
9 | | - | 'DatabaseCannotBeDecrypted' |
10 | | - | 'DatabaseNotOpen' |
11 | | - | 'UnableToAttachToDatabase' |
12 | | - | 'SqlExecutionError' |
13 | | - | 'CouldNotLoadFile' |
14 | | - | 'NoBatchCommandsProvided' |
| 39 | + (typeof NitroSQLiteExceptionType)[keyof typeof NitroSQLiteExceptionType] |
15 | 40 |
|
16 | 41 | /** Error thrown by managed NitroSQLite operations. Native errors are wrapped with this class. */ |
17 | 42 | export default class NitroSQLiteError extends Error { |
18 | | - /** Native exception category, if this error originated in NitroSQLite's C++ code. */ |
| 43 | + /** |
| 44 | + * Recognized native category from {@linkcode NitroSQLiteExceptionType}, or |
| 45 | + * `undefined` for JavaScript failures and unrecognized native categories. |
| 46 | + */ |
19 | 47 | readonly type: NitroSQLiteExceptionType | undefined |
20 | 48 |
|
21 | 49 | /** Create an error with a message and optional cause. */ |
@@ -71,18 +99,5 @@ function getNativeExceptionType( |
71 | 99 | if (typeEnd === -1) return undefined |
72 | 100 |
|
73 | 101 | const type = message.slice(typeStart, typeEnd) |
74 | | - switch (type) { |
75 | | - case 'UnknownError': |
76 | | - case 'DatabaseCannotBeOpened': |
77 | | - case 'EncryptionNotEnabled': |
78 | | - case 'DatabaseCannotBeDecrypted': |
79 | | - case 'DatabaseNotOpen': |
80 | | - case 'UnableToAttachToDatabase': |
81 | | - case 'SqlExecutionError': |
82 | | - case 'CouldNotLoadFile': |
83 | | - case 'NoBatchCommandsProvided': |
84 | | - return type |
85 | | - default: |
86 | | - return undefined |
87 | | - } |
| 102 | + return Object.values(NitroSQLiteExceptionType).find((value) => value === type) |
88 | 103 | } |
0 commit comments