Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(connection): handle stale connection state

t8y2b06ab4f27f05
diff --git a/src/lib/connectionHealth.ts b/src/lib/connectionHealth.ts
new file mode 100644
index 0000000000..51b75f1492
--- /dev/null
+++ b/src/lib/connectionHealth.ts
@@ -0,0 +1,25 @@
+const CONNECTION_ERROR_PATTERNS = [
+ "connection",
+ "broken pipe",
+ "reset by peer",
+ "timed out",
+ "closed",
+ "eof",
+ "i/o error",
+];
+
+export function staleConnectionMessage(error: unknown): string {
+ if (error instanceof Error) return error.message;
+ return String(error);
+}
+
+export function shouldMarkDisconnected(error: unknown): boolean {
+ const message = staleConnectionMessage(error).toLowerCase();
+ return CONNECTION_ERROR_PATTERNS.some((pattern) => message.includes(pattern)) || hasConnectionOsError(message);
+}
+
+function hasConnectionOsError(message: string): boolean {
+ const osErrorCodes = new Set(["10053", "10054", "10057", "10058", "10060", "10061"]);
+ const match = message.match(/os error\s+(\d+)/);
+ return !!match && osErrorCodes.has(match[1]);
+}
diff --git a/tests/connectionHealth.test.ts b/tests/connectionHealth.test.ts
new file mode 100644
index 0000000000..46948f4e96
--- /dev/null
+++ b/tests/connectionHealth.test.ts
@@ -0,0 +1,19 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { staleConnectionMessage, shouldMarkDisconnected } from "../src/lib/connectionHealth.ts";
+
+test("metadata connection errors should turn off connected state", () => {
+ assert.equal(shouldMarkDisconnected(new Error("connection closed by server")), true);
+ assert.equal(shouldMarkDisconnected("broken pipe"), true);
+ assert.equal(shouldMarkDisconnected("operation timed out"), true);
+});
+
+test("non-connection metadata errors should not turn off connected state", () => {
+ assert.equal(shouldMarkDisconnected(new Error("permission denied for table users")), false);
+ assert.equal(shouldMarkDisconnected("syntax error near SELECT"), false);
+});
+
+test("stale connection errors keep the original backend message", () => {
+ assert.equal(staleConnectionMessage(new Error("connection reset by peer")), "connection reset by peer");
+ assert.equal(staleConnectionMessage("closed"), "closed");
+});