How do I make a writer for JS error object to send thru the wire? When I do (pr-str (js/Error. "OOPS"))
it gives me "#object[Error Error: OOPS]"
. And (js->clj (js/Error. "Oops!"))
gives something like #object[Error Error: Oops!]
.
I want to make a writer for JS errors so I can send them thru the wire (maybe EDN) and deserialize at the other end.
ClojureScript does not attempt to serialize JS objects with a constructor, apart from some exceptions, like js/Date
.
In the case of js/Error
, it makes sense to first ask: how would this be solved in the JS world?
One answer is available here: https://stackoverflow.com/a/26199752/564509
(.stringify js/JSON (.getOwnPropertyNames js/Object err))
If you need the error to be serialized not as JSON but as EDN, you just have to iterate over the error's own property names yourself and fill in the data structure. Something like
(defn err->edn [e]
(into {}
(map (fn [k]
[(keyword k) (js->clj (gobject/get e k))]))
(.getOwnPropertyNames js/Object e)))
where gobject
is required as [goog.object :as gobject]
.