From 318eac953f4c9b963e9110fa730ceff3d102033a Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Sun, 6 Oct 2024 21:59:43 -0300 Subject: [PATCH] Improve error handling --- packages/db/src/core/cli/migration-queries.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/db/src/core/cli/migration-queries.ts b/packages/db/src/core/cli/migration-queries.ts index fceb063e9c9a..94ec3763d0e1 100644 --- a/packages/db/src/core/cli/migration-queries.ts +++ b/packages/db/src/core/cli/migration-queries.ts @@ -36,6 +36,7 @@ import type { TextColumn, } from '../types.js'; import type { RemoteDatabaseInfo, Result } from '../utils.js'; +import { LibsqlError } from '@libsql/client'; const sqlite = new SQLiteAsyncDialect(); const genTempTableName = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10); @@ -450,10 +451,19 @@ async function getDbCurrentSnapshot( ); return JSON.parse(res.snapshot); - } catch (error: any) { - if (error.code === 'SQLITE_UNKNOWN' || (error.code === 'SQLITE_ERROR' && error.rawCode === 1)) { + } catch (error) { + // Don't handle errors that are not from libSQL + if (error instanceof LibsqlError && // If the schema was never pushed to the database yet the table won't exist. // Treat a missing snapshot table as an empty table. + ( + // When connecting to a remote database in that condition + // the query will fail with the following error code and message. + (error.code === 'SQLITE_UNKNOWN' && error.message === 'SQLITE_UNKNOWN: SQLite error: no such table: _astro_db_snapshot') || + // When connecting to a local or in-memory database that does not have a snapshot table yet + // the query will fail with the following error code and message. + (error.code === 'SQLITE_ERROR' && error.message === 'SQLITE_ERROR: no such table: _astro_db_snapshot')) + ) { return; }