|
| 1 | +const NITRO_SQLITE_ERROR_NAME = 'NitroSQLiteError' |
| 2 | + |
| 3 | +/** |
| 4 | + * Custom error class for NitroSQLite operations |
| 5 | + * Extends the native Error class with proper prototype chain and error handling |
| 6 | + */ |
| 7 | +export default class NitroSQLiteError extends Error { |
| 8 | + constructor(message: string, options?: ErrorOptions) { |
| 9 | + super(message, options) |
| 10 | + this.name = NITRO_SQLITE_ERROR_NAME |
| 11 | + |
| 12 | + // Maintains proper prototype chain for instanceof checks |
| 13 | + Object.setPrototypeOf(this, NitroSQLiteError.prototype) |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Converts an unknown error to a NitroSQLiteError |
| 18 | + * Preserves stack traces and error causes when available |
| 19 | + */ |
| 20 | + static fromError(error: unknown): NitroSQLiteError { |
| 21 | + if (error instanceof NitroSQLiteError) { |
| 22 | + return error |
| 23 | + } |
| 24 | + |
| 25 | + if (error instanceof Error) { |
| 26 | + const nitroSQLiteError = new NitroSQLiteError(error.message, { |
| 27 | + cause: error.cause, |
| 28 | + }) |
| 29 | + // Preserve original stack trace if available |
| 30 | + if (error.stack) { |
| 31 | + nitroSQLiteError.stack = error.stack |
| 32 | + } |
| 33 | + return nitroSQLiteError |
| 34 | + } |
| 35 | + |
| 36 | + if (typeof error === 'string') { |
| 37 | + return new NitroSQLiteError(error) |
| 38 | + } |
| 39 | + |
| 40 | + return new NitroSQLiteError('Unknown error occurred', { |
| 41 | + cause: error, |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Converts a native error (from C++ bridge) to a NitroSQLiteError |
| 47 | + * Alias for fromError for semantic clarity |
| 48 | + */ |
| 49 | + static fromNativeError(error: unknown): NitroSQLiteError { |
| 50 | + return NitroSQLiteError.fromError(error) |
| 51 | + } |
| 52 | +} |
0 commit comments