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

0 votes
in ClojureScript by

When throwing an ex-info in ClojureScript, the default behavior is to print a verbose, cryptic version of the object. This is because Java uses a message field where JavaScript expects an error field. An easy solution is to do (set! (.-error ex) (.-message ex)); duplicating the message field into error. Doing so results in a much clearer exception display in browsers. I recommend duplication so that downstream users of ex-info can still rely on message existing. Would this be a good change for ClojureScript?

2 Answers

0 votes
by
selected ago by
 
Best answer

We can add it in here:

https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L11479

But I can't find any reference to Error#error field in MDN

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#instance_properties

Do you have some reference doc about .error field?

by
To observe the behavior is to try throwing these two expressions:

    (throw (ex-info "oh no" {}))

    (throw (set! (.-error (ex-info "oh no" {})) "oh no"))

And observe that the second form is more browser friendly (I'm using Chrome).

@Enzzo
Yes, I agree that is a good spot to add it... I've created https://github.com/clojure/clojurescript/pull/107

I agree that it is strange that this behavior is not documented, I couldn't find a reference for why. It would be good to understand that so I'll do some more research but so far I've not found anything.
by
Hmmm just thinking about this some more; maybe the issue is with the way ClojureScript overrides the toString:

    (set! (.. ExceptionInfo -prototype -toString)
      (fn []
        (this-as this (pr-str* this))))

^^ The default js/Error toString is the name + the message

However I tried overwriting this with the default toString and didn't see any difference, so either my methodology is wrong, or toString is not relevant. The way I tried overwriting it was putting this in the code before I throw the exception:

    (set! (.. ExceptionInfo -prototype -toString)
          (fn []
            (this-as this (str "z" (.-name this) (.-message this)))))

/shrug
by
Oh actually `pr-writer-ex-info` seems to be where the additional formatting is introduced for exceptions that flow through to the console.
The override of toString does kick in for printing exceptions.
So it looks like it is intentional, with the goal of exposing data and cause when present.
I'll close the PR.
0 votes
by

There's ex-message (in core) which is portable between Clojure and ClojureScript.

This is actually something ClojureScript had first that was later back-ported to Clojure.

...