Add helpful thaw exceptions

This commit is contained in:
Peter Taoussanis 2013-06-12 15:37:07 +07:00
parent 611ab56b74
commit 9a38a12e11
3 changed files with 21 additions and 8 deletions

View file

@ -2,7 +2,7 @@ Current [semantic](http://semver.org/) version:
```clojure ```clojure
[com.taoensso/nippy "1.2.1"] ; Stable [com.taoensso/nippy "1.2.1"] ; Stable
[com.taoensso/nippy "1.3.0-alpha2"] ; Development (adds crypto support) [com.taoensso/nippy "1.3.0-alpha3"] ; Development (adds crypto support!)
``` ```
# Nippy, a Clojure serialization library # Nippy, a Clojure serialization library

View file

@ -1,4 +1,4 @@
(defproject com.taoensso/nippy "1.3.0-alpha2" (defproject com.taoensso/nippy "1.3.0-alpha3"
:description "Clojure serialization library" :description "Clojure serialization library"
:url "https://github.com/ptaoussanis/nippy" :url "https://github.com/ptaoussanis/nippy"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"

View file

@ -258,12 +258,25 @@
:or {compressed? true :or {compressed? true
read-eval? false ; For `read-string` injection safety - NB!!! read-eval? false ; For `read-string` injection safety - NB!!!
}}] }}]
(try
(-> (let [ba (if password (crypto/decrypt-aes128 password ba) ba) (-> (let [ba (if password (crypto/decrypt-aes128 password ba) ba)
ba (if compressed? (utils/uncompress-bytes ba) ba)] ba (if compressed? (utils/uncompress-bytes ba) ba)]
ba) ba)
(ByteArrayInputStream.) (ByteArrayInputStream.)
(DataInputStream.) (DataInputStream.)
(thaw-from-stream! read-eval?))) (thaw-from-stream! read-eval?))
(catch Exception e
(throw (Exception.
(cond password "Thaw failed. Unencrypted data or bad password?"
compressed? "Thaw failed. Encrypted or uncompressed data?"
:else "Thaw failed. Encrypted and/or compressed data?")
e)))))
(comment
(-> (freeze-to-bytes "my data" :password [:salted "password"])
(thaw-from-bytes))
(-> (freeze-to-bytes "my data" :compress? true)
(thaw-from-bytes :compressed? false)))
(def stress-data (def stress-data
"Reference data used for tests & benchmarks." "Reference data used for tests & benchmarks."