diff --git a/TablePro/Core/Database/DatabaseManager.swift b/TablePro/Core/Database/DatabaseManager.swift index 45f096a41..1ec1a86b9 100644 --- a/TablePro/Core/Database/DatabaseManager.swift +++ b/TablePro/Core/Database/DatabaseManager.swift @@ -114,7 +114,20 @@ final class DatabaseManager { } // Create appropriate driver with effective connection - let driver = try DatabaseDriverFactory.createDriver(for: effectiveConnection) + let driver: DatabaseDriver + do { + driver = try DatabaseDriverFactory.createDriver(for: effectiveConnection) + } catch { + // Close tunnel if SSH was established + if connection.sshConfig.enabled { + Task { + try? await SSHTunnelManager.shared.closeTunnel(connectionId: connection.id) + } + } + activeSessions.removeValue(forKey: connection.id) + currentSessionId = nil + throw error + } do { try await driver.connect() diff --git a/TablePro/Views/Connection/ConnectionFormView.swift b/TablePro/Views/Connection/ConnectionFormView.swift index b6abb4b49..a2de72222 100644 --- a/TablePro/Views/Connection/ConnectionFormView.swift +++ b/TablePro/Views/Connection/ConnectionFormView.swift @@ -1014,6 +1014,13 @@ struct ConnectionFormView: View { } else { Self.logger.error( "Failed to connect: \(error.localizedDescription, privacy: .public)") + NSApplication.shared.closeWindows(withId: "main") + openWindow(id: "welcome") + AlertHelper.showErrorSheet( + title: String(localized: "Connection Failed"), + message: error.localizedDescription, + window: nil + ) } } } @@ -1035,6 +1042,13 @@ struct ConnectionFormView: View { } catch { Self.logger.error( "Failed to connect after plugin install: \(error.localizedDescription, privacy: .public)") + NSApplication.shared.closeWindows(withId: "main") + openWindow(id: "welcome") + AlertHelper.showErrorSheet( + title: String(localized: "Connection Failed"), + message: error.localizedDescription, + window: nil + ) } } }