Skip to content

Commit

Permalink
changed exception type and message
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacervello committed Aug 12, 2017
1 parent 1cd2760 commit 36ae914
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/cheshire/exact.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
(catch Exception _ false))]
(if valid-json?
parsed
(throw (Exception. "Invalid json")))))
(throw
(IllegalArgumentException.
"Invalid JSON, expected exactly one parseable object but multiple objects were found")))))

(defn parse-string
"Like cheshire.core/parse-string
Expand Down
25 changes: 13 additions & 12 deletions test/cheshire/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,16 @@
(is (= {:a 1} (json/decode "{\"a\": 1}" (Boolean. true)))))

(deftest t-invalid-json
(are [x y] (= x (try
y
(catch Exception e
(.getMessage e))))
"Invalid json" (json-exact/decode "{\"foo\": 1}asdf")
"Invalid json" (json-exact/decode "{\"foo\": 123}null")
"Invalid json" (json-exact/decode "\"hello\" : 123}")
{"foo" 1} (json/decode "{\"foo\": 1}")
"Invalid json" (json-exact/decode-strict "{\"foo\": 1}asdf")
"Invalid json" (json-exact/decode-strict "{\"foo\": 123}null")
"Invalid json" (json-exact/decode-strict "\"hello\" : 123}")
{"foo" 1} (json/decode-strict "{\"foo\": 1}")))
(let [invalid-json-message "Invalid JSON, expected exactly one parseable object but multiple objects were found"]
(are [x y] (= x (try
y
(catch Exception e
(.getMessage e))))
invalid-json-message (json-exact/decode "{\"foo\": 1}asdf")
invalid-json-message (json-exact/decode "{\"foo\": 123}null")
invalid-json-message (json-exact/decode "\"hello\" : 123}")
{"foo" 1} (json/decode "{\"foo\": 1}")
invalid-json-message (json-exact/decode-strict "{\"foo\": 1}asdf")
invalid-json-message (json-exact/decode-strict "{\"foo\": 123}null")
invalid-json-message (json-exact/decode-strict "\"hello\" : 123}")
{"foo" 1} (json/decode-strict "{\"foo\": 1}"))))

0 comments on commit 36ae914

Please sign in to comment.