Skip to content

Commit

Permalink
Give connection errors friendlier error messages
Browse files Browse the repository at this point in the history
- Wrap connection in try catch block
- Add tests for expected exceptions
- Add docstring
- Update CHANGELOG

Fixes #41
  • Loading branch information
danielcompton committed Jul 3, 2015
1 parent da5ec02 commit 9ff607c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ All notable changes to this project will be documented in this file. This change

### Changed
- Add new arity for the queries `table-drop`, and `table-list` which doesn't require a db. [#54](https://github.com/apa512/clj-rethinkdb/pull/54/files)
- Add docstring to `rethinkdb.query` ns explaining DB priority [#54](https://github.com/apa512/clj-rethinkdb/pull/54/files)
- Add docstring to `rethinkdb.query` ns explaining DB priority [#54](https://github.com/apa512/clj-rethinkdb/pull/54)
- Exceptions thrown when connecting are more descriptive, and are now of type `clojure.lang.ExceptionInfo`. [#41](https://github.com/apa512/clj-rethinkdb/issues/41) [#56](https://github.com/apa512/clj-rethinkdb/pull/56)

### Fixed
- Fix close method on Connection record [#50](https://github.com/apa512/clj-rethinkdb/pull/50)
Expand Down
43 changes: 23 additions & 20 deletions src/rethinkdb/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,26 @@
token 0
auth-key ""
db nil}}]
(let [socket (Socket. host port)
out (DataOutputStream. (.getOutputStream socket))
in (DataInputStream. (.getInputStream socket))]
;; Initialise the connection
(send-version out)
(send-auth-key out auth-key)
(send-protocol out)
(let [init-response (read-init-response in)]
(if-not (= init-response "SUCCESS")
(throw (Exception. init-response))))
;; Once initialised, create the connection record
(connection
(merge
{:socket socket
:out out
:in in
:db db
:waiting #{}
:token token}
(make-connection-loops in out)))))
(try
(let [socket (Socket. host port)
out (DataOutputStream. (.getOutputStream socket))
in (DataInputStream. (.getInputStream socket))]
;; Initialise the connection
(send-version out)
(send-auth-key out auth-key)
(send-protocol out)
(let [init-response (read-init-response in)]
(if-not (= init-response "SUCCESS")
(throw (ex-info init-response {:host host :port port :auth-key auth-key :db db}))))
;; Once initialised, create the connection record
(connection
(merge
{:socket socket
:out out
:in in
:db db
:waiting #{}
:token token}
(make-connection-loops in out))))
(catch Exception e
(throw (ex-info "Error connecting to RethinkDB database" {:host host :port port :auth-key auth-key :db db} e)))))
3 changes: 2 additions & 1 deletion test/rethinkdb/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@

(deftest query-conn
(is (do (r/connect)
true)))
true))
(is (thrown? clojure.lang.ExceptionInfo (r/connect :port 1))))

(use-fixtures :each setup-each)
(use-fixtures :once setup-once)

0 comments on commit 9ff607c

Please sign in to comment.