From aa08f4b58c50c2583022c7ba0591f0203812bff6 Mon Sep 17 00:00:00 2001 From: Chris Broome Date: Mon, 3 Dec 2018 22:12:45 -0500 Subject: [PATCH 1/4] 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))))) From 9d34fc02316efc75694e841d40b48e6cee05a309 Mon Sep 17 00:00:00 2001 From: Chris Broome Date: Tue, 4 Dec 2018 21:20:25 -0500 Subject: [PATCH 2/4] Throw IllegalArgumentException when database name not in uri --- src/clojure/monger/core.clj | 4 ++-- test/monger/test/core_test.clj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/clojure/monger/core.clj b/src/clojure/monger/core.clj index 5573bc6..fe24ec1 100644 --- a/src/clojure/monger/core.clj +++ b/src/clojure/monger/core.clj @@ -236,8 +236,8 @@ (let [uri (MongoClientURI. uri-string) conn (MongoClient. uri) dbName (.getDatabase uri)] - (if (nil? dbName) - (throw (Exception. "No database name specified in uri")) + (if (not dbName) + (throw (IllegalArgumentException. "No database name specified in uri")) (let [db (.getDB conn dbName)] {:conn conn :db db})))) diff --git a/test/monger/test/core_test.clj b/test/monger/test/core_test.clj index c8082ce..02f45dc 100644 --- a/test/monger/test/core_test.clj +++ b/test/monger/test/core_test.clj @@ -73,4 +73,4 @@ (deftest connect-to-uri-without-db-name (let [uri "mongodb://localhost:27017"] - (is (thrown? Exception (mg/connect-via-uri uri))))) + (is (thrown? IllegalArgumentException (mg/connect-via-uri uri))))) From 8102c42888ed797cb1b77c0c4dce57e2199c02dd Mon Sep 17 00:00:00 2001 From: Chris Broome Date: Tue, 4 Dec 2018 22:33:34 -0500 Subject: [PATCH 3/4] TravisCI: change dist to xenial - Change distribution to `xenial` - Remove custom mongodb 4.0 installation since xenial ships with 4.0 - Increase wait time from 5 to 15 seconds --- .travis.yml | 2 +- bin/ci/before_script.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d9e866d..62d76e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: clojure sudo: required lein: lein +dist: xenial before_script: - - ./bin/ci/install_mongodb.sh - mongod --version - ./bin/ci/before_script.sh script: lein do clean, javac, test diff --git a/bin/ci/before_script.sh b/bin/ci/before_script.sh index 711d6dd..77f1a97 100755 --- a/bin/ci/before_script.sh +++ b/bin/ci/before_script.sh @@ -1,7 +1,7 @@ #!/bin/sh # MongoDB seems to need some time to boot first. MK. -sleep 5 +sleep 15 # MongoDB Java driver won't run authentication twice on the same DB instance, # so we need to use multiple DBs. From 900592e302c40d30457500a05cac262b7ffbf382 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Fri, 7 Dec 2018 19:51:59 +0300 Subject: [PATCH 4/4] Cosmetics, wording --- src/clojure/monger/core.clj | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/clojure/monger/core.clj b/src/clojure/monger/core.clj index fe24ec1..e5314a0 100644 --- a/src/clojure/monger/core.clj +++ b/src/clojure/monger/core.clj @@ -234,12 +234,10 @@ If username and password are provided, performs authentication." [^String uri-string] (let [uri (MongoClientURI. uri-string) - conn (MongoClient. uri) - dbName (.getDatabase uri)] - (if (not dbName) - (throw (IllegalArgumentException. "No database name specified in uri")) - (let [db (.getDB conn dbName)] - {:conn conn :db db})))) + conn (MongoClient. uri)] + (if-let [dbName (.getDatabase uri)] + {:conn conn :db (.getDB conn dbName)} + (throw (IllegalArgumentException. "No database name specified in URI. Monger requires a database to be explicitly configured."))))) (defn ^com.mongodb.CommandResult command "Runs a database command (please check MongoDB documentation for the complete list of commands).