Skip to content

Commit

Permalink
[#8] records not being printed usefully on invariant violations
Browse files Browse the repository at this point in the history
Before commit, records were printing like `my.namespace.MyRecord@aa4acce8`.

This change also affects printing of lazy seqs, etc.
Note that we're currently just depending on pre-existing *print-length*
value here, so care should be taken with validating infinite lazy seqs.
  • Loading branch information
ptaoussanis committed Mar 27, 2017
1 parent 9c7fb29 commit 9f1db3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/taoensso/truss.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
(have? [:set<= [:a :b :c]] #{:a :b})
(qb 10000
(with-error-fn nil (have? string? 5))
(with-error-fn (fn [_] :truss/error) (have? string? 5))))
(with-error-fn (fn [_] :truss/error) (have? string? 5)))

(have string? (range 1000)))

(comment
;; HotSpot is great with these:
Expand Down
21 changes: 18 additions & 3 deletions src/taoensso/truss/impl.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,22 @@
;; Cider unfortunately doesn't seem to print newlines in errors
(str "Invariant violation in `" x1 ":" x2 "`. Test form `" x3 "` with failing input: `" x4 "`"))

#+clj
(defn- fast-pr-str
"Combination `with-out-str`, `pr`. Ignores *print-dup*."
[x]
(let [w (java.io.StringWriter.)]
(print-method x w)
(.toString w)))

(comment (enc/qb 1e5 (pr-str {:a :A}) (fast-pr-str {:a :A})))

(deftype WrappedError [val])
(defn -assertion-error [msg] #+clj (AssertionError. msg) #+cljs (js/Error. msg))
(def -dummy-val #+clj (Object.) #+cljs (js-obj))
(def -dummy-error #+clj (Object.) #+cljs (js-obj))
(defn -invar-violation!
;; - http://dev.clojure.org/jira/browse/CLJ-865 would be handy for line numbers.
;; - Clojure 1.7+'s `pr-str` dumps a ton of error info that we don't want here.
[elidable? ns-str ?line form val ?err ?data-fn]
(when-let [error-fn *error-fn*]
(error-fn ; Nb consumer must deref while bindings are still active
Expand All @@ -163,7 +172,12 @@
(cond
undefn-val? "<undefined>"
(nil? val) "<nil>"
:else (str val) #_(pr-str val))
:else
#_(str val)
#_(pr-str val)
;; Consider setting *print-length* for lazy seqs?
#+clj (fast-pr-str val)
#+cljs (pr-str val))

?err
(cond
Expand All @@ -174,7 +188,8 @@

msg_
(delay
(let [?err-str (when-let [e ?err] (str ?err) #_(pr-str ?err))
(let [;; Clj 1.7+ `pr-str` dumps a ton of error info that we don't want here
?err-str (when-let [e ?err] (str ?err) #_(pr-str ?err))
msg (fmt-err-msg ns-str line-str form-str val-str)]
(cond
(not ?err) msg
Expand Down

0 comments on commit 9f1db3d

Please sign in to comment.