From 3ab91763c6818ccee92d2da7a7e4ac0ab80dea2c Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Thu, 14 Apr 2016 00:13:33 +0700 Subject: [PATCH] [#82] Make it easier to spot new->old Nippy thaw failures --- src/taoensso/nippy.clj | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 465ab97..8760c6f 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -962,20 +962,21 @@ (def ^:private class-method-sig (into-array Class [IPersistentMap])) -(defn- read-custom! [in type-id] +(defn- read-custom! [in prefixed? type-id] (if-let [custom-reader (get *custom-readers* type-id)] (try (custom-reader in) (catch Exception e (throw (ex-info - (str "Reader exception for custom type with internal id: " - type-id) {:internal-type-id type-id} e)))) + (str "Reader exception for custom type id: " type-id) + {:type-id type-id + :prefixed? prefixed?} e)))) (throw (ex-info - (str "No reader provided for custom type with internal id: " - type-id) - {:internal-type-id type-id})))) + (str "No reader provided for custom type id: " type-id) + {:type-id type-id + :prefixed? prefixed?})))) (defn- read-edn [edn] (try @@ -1127,9 +1128,14 @@ (fn [] (thaw-from-in! in)))) ;; ----------------------------------------------------------------- - id-prefixed-custom (read-custom! in (.readShort in)) - (read-custom! in type-id) ; Unprefixed custom type (catchall) - ) + id-prefixed-custom (read-custom! in :prefixed (.readShort in)) + + (if (neg? type-id) + (read-custom! in nil type-id) ; Unprefixed custom type + (throw + (ex-info + (str "Unrecognized type id (" type-id "). Data frozen with newer Nippy version?") + {:type-id type-id})))) (catch Exception e (throw (ex-info (str "Thaw failed against type-id: " type-id) @@ -1314,7 +1320,7 @@ ;; Prefixed [const byte id][cust hash id][payload]: (do (write-id ~out ~id-prefixed-custom) (.writeShort ~out ~(coerce-custom-type-id custom-type-id)))) - ~@body))) + ~@body))) (defmacro extend-thaw "Extends Nippy to support thawing of a custom type with given id: