From aa08f4b58c50c2583022c7ba0591f0203812bff6 Mon Sep 17 00:00:00 2001 From: Chris Broome Date: Mon, 3 Dec 2018 22:12:45 -0500 Subject: [PATCH] Add more descriptive error message when uri has no db name --- src/clojure/monger/core.clj | 11 +++++++---- test/monger/test/core_test.clj | 4 ++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/clojure/monger/core.clj b/src/clojure/monger/core.clj index 880a259..5573bc6 100644 --- a/src/clojure/monger/core.clj +++ b/src/clojure/monger/core.clj @@ -233,10 +233,13 @@ Commonly used for PaaS-based applications, for example, running on Heroku. If username and password are provided, performs authentication." [^String uri-string] - (let [uri (MongoClientURI. uri-string) - conn (MongoClient. uri) - db (.getDB conn (.getDatabase uri))] - {:conn conn :db db})) + (let [uri (MongoClientURI. uri-string) + conn (MongoClient. uri) + dbName (.getDatabase uri)] + (if (nil? dbName) + (throw (Exception. "No database name specified in uri")) + (let [db (.getDB conn dbName)] + {:conn conn :db db})))) (defn ^com.mongodb.CommandResult command "Runs a database command (please check MongoDB documentation for the complete list of commands). diff --git a/test/monger/test/core_test.clj b/test/monger/test/core_test.clj index eeaa5b6..c8082ce 100644 --- a/test/monger/test/core_test.clj +++ b/test/monger/test/core_test.clj @@ -70,3 +70,7 @@ :cursor-finalizer-enabled true :required-replica-set-name "rs"}] (is (instance? com.mongodb.MongoClientOptions$Builder (mg/mongo-options-builder opts))))) + +(deftest connect-to-uri-without-db-name + (let [uri "mongodb://localhost:27017"] + (is (thrown? Exception (mg/connect-via-uri uri)))))