diff --git a/.changeset/breezy-radios-grab.md b/.changeset/breezy-radios-grab.md new file mode 100644 index 0000000000..afb990a449 --- /dev/null +++ b/.changeset/breezy-radios-grab.md @@ -0,0 +1,5 @@ +--- +'@astrojs/db': patch +--- + +Fixes `isDbError()` guard for `LibsqlError` diff --git a/packages/db/src/core/cli/commands/execute/index.ts b/packages/db/src/core/cli/commands/execute/index.ts index 9a5a1b8e2f..66ecdd774b 100644 --- a/packages/db/src/core/cli/commands/execute/index.ts +++ b/packages/db/src/core/cli/commands/execute/index.ts @@ -1,5 +1,4 @@ import { existsSync } from 'node:fs'; -import { LibsqlError } from '@libsql/client'; import type { AstroConfig } from 'astro'; import { green } from 'kleur/colors'; import type { Arguments } from 'yargs-parser'; @@ -16,6 +15,7 @@ import { import { bundleFile, importBundledFile } from '../../../load-file.js'; import type { DBConfig } from '../../../types.js'; import { getManagedRemoteToken } from '../../../utils.js'; +import { isDbError } from '../../../../runtime/utils.js'; export async function cmd({ astroConfig, @@ -64,9 +64,7 @@ export async function cmd({ await mod.default(); console.info(`${green('✔')} File run successfully.`); } catch (e) { - if (e instanceof LibsqlError) { - throw new Error(EXEC_ERROR(e.message)); - } - throw e; + if (isDbError(e)) throw new Error(EXEC_ERROR(e.message)); + else throw e; } } diff --git a/packages/db/src/core/cli/migration-queries.ts b/packages/db/src/core/cli/migration-queries.ts index bd6360665d..db3972d095 100644 --- a/packages/db/src/core/cli/migration-queries.ts +++ b/packages/db/src/core/cli/migration-queries.ts @@ -1,5 +1,4 @@ import { stripVTControlCharacters } from 'node:util'; -import { LibsqlError } from '@libsql/client'; import deepDiff from 'deep-diff'; import { sql } from 'drizzle-orm'; import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core'; @@ -8,7 +7,7 @@ import { customAlphabet } from 'nanoid'; import { hasPrimaryKey } from '../../runtime/index.js'; import { createRemoteDatabaseClient } from '../../runtime/index.js'; import { isSerializedSQL } from '../../runtime/types.js'; -import { safeFetch } from '../../runtime/utils.js'; +import { isDbError, safeFetch } from '../../runtime/utils.js'; import { MIGRATION_VERSION } from '../consts.js'; import { RENAME_COLUMN_ERROR, RENAME_TABLE_ERROR } from '../errors.js'; import { @@ -454,7 +453,7 @@ async function getDbCurrentSnapshot( } catch (error) { // Don't handle errors that are not from libSQL if ( - error instanceof LibsqlError && + isDbError(error) && // If the schema was never pushed to the database yet the table won't exist. // Treat a missing snapshot table as an empty table. diff --git a/packages/db/src/core/integration/index.ts b/packages/db/src/core/integration/index.ts index 200c7ddc25..51d3f80116 100644 --- a/packages/db/src/core/integration/index.ts +++ b/packages/db/src/core/integration/index.ts @@ -3,7 +3,6 @@ import { mkdir, writeFile } from 'node:fs/promises'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import type { ManagedAppToken } from '@astrojs/studio'; -import { LibsqlError } from '@libsql/client'; import type { AstroIntegration } from 'astro'; import { blue, yellow } from 'kleur/colors'; import { @@ -15,7 +14,7 @@ import { mergeConfig, } from 'vite'; import parseArgs from 'yargs-parser'; -import { AstroDbError } from '../../runtime/utils.js'; +import { AstroDbError, isDbError } from '../../runtime/utils.js'; import { CONFIG_FILE_NAMES, DB_PATH, VIRTUAL_MODULE_ID } from '../consts.js'; import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from '../errors.js'; import { resolveDbConfig } from '../load-file.js'; @@ -206,7 +205,7 @@ async function executeSeedFile({ try { await mod.default(); } catch (e) { - if (e instanceof LibsqlError) { + if (isDbError(e)) { throw new AstroDbError(EXEC_ERROR(e.message)); } throw e; diff --git a/packages/db/src/runtime/utils.ts b/packages/db/src/runtime/utils.ts index 64301c39ee..74201957ff 100644 --- a/packages/db/src/runtime/utils.ts +++ b/packages/db/src/runtime/utils.ts @@ -42,6 +42,10 @@ export class DetailedLibsqlError extends LibsqlError { } } +export function isDbError(err: unknown): err is LibsqlError { + return err instanceof LibsqlError || (err instanceof Error && (err as any).libsqlError === true) +} + function slash(path: string) { const isExtendedLengthPath = path.startsWith('\\\\?\\'); diff --git a/packages/db/src/runtime/virtual.ts b/packages/db/src/runtime/virtual.ts index 6f008fe0d8..3da0c35491 100644 --- a/packages/db/src/runtime/virtual.ts +++ b/packages/db/src/runtime/virtual.ts @@ -21,10 +21,6 @@ function createColumn>(type: }; } -export function isDbError(err: unknown): err is LibsqlError { - return err instanceof LibsqlError; -} - export const column = { number: (opts: T = {} as T) => { return createColumn('number', opts) satisfies { type: 'number' }; @@ -90,3 +86,4 @@ export { } from 'drizzle-orm'; export { alias } from 'drizzle-orm/sqlite-core'; +export { isDbError } from './utils.js'; diff --git a/packages/db/test/test-utils.js b/packages/db/test/test-utils.js index 8315e85512..b608d75b8b 100644 --- a/packages/db/test/test-utils.js +++ b/packages/db/test/test-utils.js @@ -1,9 +1,10 @@ import { createServer } from 'node:http'; -import { LibsqlError, createClient } from '@libsql/client'; +import { createClient } from '@libsql/client'; import { z } from 'zod'; import { cli } from '../dist/core/cli/index.js'; import { resolveDbConfig } from '../dist/core/load-file.js'; import { getCreateIndexQueries, getCreateTableQuery } from '../dist/core/queries.js'; +import { isDbError } from '../dist/runtime/utils.js'; const singleQuerySchema = z.object({ sql: z.string(), @@ -142,7 +143,7 @@ function createRemoteDbServer() { JSON.stringify({ success: false, error: { - code: e instanceof LibsqlError ? e.code : 'SQLITE_QUERY_FAILED', + code: isDbError(e) ? e.code : 'SQLITE_QUERY_FAILED', details: e.message, }, }),