A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.
C++
fix(mongodb): default authSource=admin and surface server error message (#14)
Tobi7042ecb8be88
diff --git a/Package.swift b/Package.swiftindex 083b057..c62aec2 100644--- a/Package.swift+++ b/Package.swift@@ -36,6 +36,7 @@ let package = Package(.product(name: "MySQLNIO", package: "mysql-nio"),.product(name: "RediStack", package: "RediStack"),.product(name: "MongoKitten", package: "MongoKitten"),+ .product(name: "MongoClient", package: "MongoKitten"),.product(name: "CosmoMSSQL", package: "CosmoSQLClient-Swift"),.product(name: "NIOSSL", package: "swift-nio-ssl"),.product(name: "NIOSSH", package: "swift-nio-ssh"),diff --git a/macos/Data/Adapters/MongoDB/MongoDBAdapter.swift b/macos/Data/Adapters/MongoDB/MongoDBAdapter.swiftindex 94a2572..e6c10c9 100644--- a/macos/Data/Adapters/MongoDB/MongoDBAdapter.swift+++ b/macos/Data/Adapters/MongoDB/MongoDBAdapter.swift@@ -11,6 +11,7 @@import Foundationimport MongoKitten+import MongoClientimport NIOCorefinal class MongoDBAdapter: DatabaseAdapter, @unchecked Sendable {@@ -33,7 +34,7 @@ final class MongoDBAdapter: DatabaseAdapter, @unchecked Sendable {self.connectionConfig = configself.isConnected = true} catch {- throw GridexError.connectionFailed(underlying: error)+ throw GridexError.connectionFailed(underlying: Self.wrap(mongoError: error))}}@@ -50,6 +51,7 @@ final class MongoDBAdapter: DatabaseAdapter, @unchecked Sendable {let port = config.port ?? 27017let dbName = config.database ?? "admin"+ let hasCredentials = !(config.username?.isEmpty ?? true)var uri = "mongodb://"if let user = config.username, !user.isEmpty {@@ -60,8 +62,19 @@ final class MongoDBAdapter: DatabaseAdapter, @unchecked Sendable {uri += "@"}uri += "\(hostField):\(port)/\(dbName)"++ // Most MongoDB deployments store users in the `admin` database. Without+ // an explicit authSource, MongoKitten authenticates against the URI's+ // database and the server returns a generic auth failure.+ var params: [String] = []+ if hasCredentials {+ params.append("authSource=admin")+ }if config.sslEnabled {- uri += "?tls=true"+ params.append("tls=true")+ }+ if !params.isEmpty {+ uri += "?" + params.joined(separator: "&")}return uri}@@ -109,6 +122,33 @@ final class MongoDBAdapter: DatabaseAdapter, @unchecked Sendable {}}+ /// MongoKitten error types lack NSError bridging, so the default+ /// `localizedDescription` is the unhelpful "operation couldn't be completed+ /// (… error 1.)". Extract `errmsg`/`codeName` from the server reply when+ /// available so the user sees the real failure reason.+ private static func wrap(mongoError error: Error) -> NSError {+ let message: String+ if let serverError = error as? MongoServerError {+ let doc = serverError.document+ let msg = (doc["errmsg"] as? String) ?? "MongoDB server returned an error"+ let codeName = (doc["codeName"] as? String).map { " (\($0))" } ?? ""+ message = msg + codeName+ } else if let reply = error as? MongoGenericErrorReply {+ let msg = reply.errorMessage ?? "MongoDB server returned an error"+ let codeName = reply.codeName.map { " (\($0))" } ?? ""+ message = msg + codeName+ } else if let described = error as? CustomStringConvertible {+ message = described.description+ } else {+ message = error.localizedDescription+ }+ return NSError(+ domain: "MongoDB",+ code: -1,+ userInfo: [NSLocalizedDescriptionKey: message]+ )+ }+func disconnect() async throws {// MongoKitten cleans up via deinit; just clear referencesdatabase = nil