Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix isDbError()-guard does not work #12416

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/breezy-radios-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/db': patch
---

fix isDbError()-guard for LibsqlError
bluwy marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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/virtual.js';
abegehr marked this conversation as resolved.
Show resolved Hide resolved

export async function cmd({
astroConfig,
Expand Down Expand Up @@ -64,7 +64,7 @@ export async function cmd({
await mod.default();
console.info(`${green('✔')} File run successfully.`);
} catch (e) {
if (e instanceof LibsqlError) {
if (isDbError(e)) {
throw new Error(EXEC_ERROR(e.message));
}
throw e;
Expand Down
4 changes: 2 additions & 2 deletions packages/db/src/core/cli/migration-queries.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -37,6 +36,7 @@ import type {
TextColumn,
} from '../types.js';
import type { RemoteDatabaseInfo, Result } from '../utils.js';
import { isDbError } from '../../runtime/virtual.js';

const sqlite = new SQLiteAsyncDialect();
const genTempTableName = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10);
Expand Down Expand Up @@ -454,7 +454,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.

Expand Down
6 changes: 3 additions & 3 deletions packages/db/src/core/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { AstroConfig, AstroIntegration } from 'astro';
import { blue, yellow } from 'kleur/colors';
import {
Expand All @@ -15,7 +14,8 @@ import {
mergeConfig,
} from 'vite';
import parseArgs from 'yargs-parser';
import { AstroDbError } from '../../runtime/utils.js';
import { AstroDbError, } from '../../runtime/utils.js';
import { isDbError } from '../../runtime/virtual.js';
import { CONFIG_FILE_NAMES, DB_PATH } from '../consts.js';
import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from '../errors.js';
import { resolveDbConfig } from '../load-file.js';
Expand Down Expand Up @@ -209,7 +209,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;
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/runtime/virtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createColumn<S extends string, T extends Record<string, unknown>>(type:
}

export function isDbError(err: unknown): err is LibsqlError {
return err instanceof LibsqlError;
return err instanceof LibsqlError || (err instanceof Error && (err as any).libsqlError === true)
}

export const column = {
Expand Down
4 changes: 2 additions & 2 deletions packages/db/test/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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';
Expand Down Expand Up @@ -142,7 +142,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',
abegehr marked this conversation as resolved.
Show resolved Hide resolved
details: e.message,
},
}),
Expand Down
Loading