Add more descriptive error message when uri has no db name

This commit is contained in:
Chris Broome 2018-12-03 22:12:45 -05:00
parent 4b591fc910
commit aa08f4b58c
No known key found for this signature in database
GPG key ID: 1069B8DCF35855B0
2 changed files with 11 additions and 4 deletions

View file

@ -233,10 +233,13 @@
Commonly used for PaaS-based applications, for example, running on Heroku. Commonly used for PaaS-based applications, for example, running on Heroku.
If username and password are provided, performs authentication." If username and password are provided, performs authentication."
[^String uri-string] [^String uri-string]
(let [uri (MongoClientURI. uri-string) (let [uri (MongoClientURI. uri-string)
conn (MongoClient. uri) conn (MongoClient. uri)
db (.getDB conn (.getDatabase uri))] dbName (.getDatabase uri)]
{:conn conn :db db})) (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 (defn ^com.mongodb.CommandResult command
"Runs a database command (please check MongoDB documentation for the complete list of commands). "Runs a database command (please check MongoDB documentation for the complete list of commands).

View file

@ -70,3 +70,7 @@
:cursor-finalizer-enabled true :cursor-finalizer-enabled true
:required-replica-set-name "rs"}] :required-replica-set-name "rs"}]
(is (instance? com.mongodb.MongoClientOptions$Builder (mg/mongo-options-builder opts))))) (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)))))