Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by
edited by

I'm getting a js/Error in my catch form of try/catch. I was doing some repl work to see what I can understand about js/Errors. What I don't understand is why (js->clj js-err) just returns the same js/Error object.

(def js-err (js/Error. "testing"))

js-err
;; => #object[Error Error: testing]
(js->clj js-err)
;; => #object[Error Error: testing]

evaluating js-err and (js->clj js-err) result in the same thing.

Here are the properties on js-err

(.getOwnPropertyNames js/Object js-err)
;; => #js["stack" "message"]

what I want or expect to happen:

(js->clj js-err)
;; => {:message "testing"
;;     :stack "blah\nblah"}

Could someone explain why js->clj doesn't give me the object data as a map, and is there a idiomatic way to accomplish turning that object into a map?

This is in the nodejs host

1 Answer

+2 votes
by
selected by
 
Best answer

js->clj only transforms regular JavaScript Object and Arrays. Error is a sub-type, so it isn't converted. You can implement the cljs.core/IEncodeClojure protocol in theory, so that it does get converted. It is not done by default since the stack information in js/Error is not standardized in any way, so each JS runtime more or less does it own thing. Handling all of them is kinda tedius and would bloat the code too much for very rare use cases.

There is a cljs.core/Throwable->map fn coming, but I believe that is unreleased as of yet. You could just copy it until then.

...