Skip to content

Commit 3418539

Browse files
committed
fix: re-throw DbError and guard ndb handle in openRepo (#671)
Re-throw DbError (e.g. DB not found) instead of silently falling back to better-sqlite3, which would produce a misleading debug message. Also wrap the ndb setup block in try/catch to close the handle if an exception occurs after openReadonly, preventing resource leaks.
1 parent ede394a commit 3418539

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

src/db/connection.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -317,31 +317,37 @@ export function openRepo(
317317
}
318318
const native = getNative();
319319
const ndb = native.NativeDatabase.openReadonly(dbPath);
320-
321-
// Version check (same logic as openReadonlyOrFail)
322-
if (!_versionWarned) {
323-
try {
324-
const buildVersion = ndb.getBuildMeta('codegraph_version');
325-
const currentVersion = getPackageVersion();
326-
if (buildVersion && currentVersion && buildVersion !== currentVersion) {
327-
warn(
328-
`DB was built with codegraph v${buildVersion}, running v${currentVersion}. Consider: codegraph build --no-incremental`,
329-
);
320+
try {
321+
// Version check (same logic as openReadonlyOrFail)
322+
if (!_versionWarned) {
323+
try {
324+
const buildVersion = ndb.getBuildMeta('codegraph_version');
325+
const currentVersion = getPackageVersion();
326+
if (buildVersion && currentVersion && buildVersion !== currentVersion) {
327+
warn(
328+
`DB was built with codegraph v${buildVersion}, running v${currentVersion}. Consider: codegraph build --no-incremental`,
329+
);
330+
}
331+
} catch {
332+
// build_meta table may not exist in older DBs
330333
}
331-
} catch {
332-
// build_meta table may not exist in older DBs
334+
_versionWarned = true;
333335
}
334-
_versionWarned = true;
335-
}
336336

337-
return {
338-
repo: new NativeRepository(ndb),
339-
close() {
340-
ndb.close();
341-
},
342-
};
337+
return {
338+
repo: new NativeRepository(ndb),
339+
close() {
340+
ndb.close();
341+
},
342+
};
343+
} catch (innerErr) {
344+
ndb.close();
345+
throw innerErr;
346+
}
343347
} catch (e) {
344-
// If native open fails (e.g. incompatible DB), fall through to better-sqlite3
348+
// Re-throw user-visible errors (e.g. DB not found) — only silently
349+
// fall back for native-engine failures (e.g. incompatible native binary).
350+
if (e instanceof DbError) throw e;
345351
debug(
346352
`openRepo: native path failed, falling back to better-sqlite3: ${(e as Error).message}`,
347353
);

0 commit comments

Comments
 (0)