From 694266a4755c293c8961e112c69f8a0c8c0803b9 Mon Sep 17 00:00:00 2001 From: Daniel Compton Date: Mon, 12 Oct 2015 10:29:27 +1300 Subject: [PATCH] Prefix RethinkDB server exceptions with `RethinkDB server: ` It is sometimes difficult for people to distinguish between an exception thrown in the client library, and an exception from the server which is sent back and thrown by the client library. This change prefixes serverside exceptions to make this distinction clearer. Fixes #95, fixes #94 --- CHANGELOG.md | 1 + src/rethinkdb/net.clj | 2 +- test/rethinkdb/core_test.clj | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5db46b..fd2875e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. This change - Disable Nagle's algorithm (set TCP NO_DELAY to true). This provides a speedup of around 30-40x for small queries on Linux. [#114](https://github.com/apa512/clj-rethinkdb/pull/114) - Added a new arity to `changes` that allows you to pass optargs. [#112](https://github.com/apa512/clj-rethinkdb/issues/112) - Renamed changes arg from `table` to `xs`. [#76](https://github.com/apa512/clj-rethinkdb/issues/76) +- Prefix RethinkDB server exceptions with `RethinkDB server:`. [#100](https://github.com/apa512/clj-rethinkdb/pull/100) ## [0.11.0] - 2015-10-19 diff --git a/src/rethinkdb/net.clj b/src/rethinkdb/net.clj index 1a130fb..83d1335 100644 --- a/src/rethinkdb/net.clj +++ b/src/rethinkdb/net.clj @@ -120,7 +120,7 @@ (do (swap! (:conn conn) update-in [:waiting] #(conj % token)) (Cursor. conn token resp))) - (let [ex (ex-info (str (first resp)) json-resp)] + (let [ex (ex-info (str "RethinkDB server: " (first resp)) json-resp)] (log/error ex) (throw ex))))) diff --git a/test/rethinkdb/core_test.clj b/test/rethinkdb/core_test.clj index 033355e..f73c0f2 100644 --- a/test/rethinkdb/core_test.clj +++ b/test/rethinkdb/core_test.clj @@ -3,7 +3,8 @@ [clojure.test :refer :all] [rethinkdb.query :as r] [rethinkdb.core :as core] - [rethinkdb.net :as net])) + [rethinkdb.net :as net]) + (:import (clojure.lang ExceptionInfo))) (def test-db "cljrethinkdb_test") (def test-table :pokedex) @@ -359,12 +360,22 @@ (r/get-field :name)) conn)))))) +(deftest throwing-server-exceptions + (with-open [conn (r/connect :db test-db)] + (is (thrown? ExceptionInfo (r/run (r/table :nope) conn))) + (try (r/run (r/table :nope) conn) + (catch ExceptionInfo ex + (let [{:keys [r]} (ex-data ex) + msg (.getMessage ex)] + (is (= r ["Table `cljrethinkdb_test.nope` does not exist."])) + (is (= "RethinkDB server: Table `cljrethinkdb_test.nope` does not exist." msg))))))) + (deftest query-conn (is (do (r/connect) true)) - (is (thrown? clojure.lang.ExceptionInfo (r/connect :port 1))) + (is (thrown? ExceptionInfo (r/connect :port 1))) (with-redefs-fn {#'core/send-version (fn [out] (net/send-int out 168696 4))} - #(is (thrown? clojure.lang.ExceptionInfo (r/connect))))) + #(is (thrown? ExceptionInfo (r/connect))))) (use-fixtures :each setup-each) (use-fixtures :once setup-once)