r/Clojure • • Aug 11 '18

Error message improvements in Clojure 1.10

https://mobile.twitter.com/puredanger/status/1028103654241443840
89 Upvotes

41 comments sorted by

View all comments

4

u/clj_user Aug 11 '18

It looks almost exactly the same. And I'd love to see a comparison to 1.8, before spec, and before the messages got worse.

22

u/alexdmiller Aug 11 '18

There are several important differences.

  • execution phase is identified (reading, macroexpand, compile, etc)

  • macroexpansion errors report the location in macro call rather than in the macro impl

  • compile or macroexpand symbol is identified

  • unuseful details are suppressed when not useful (cause class, source file in repl)

  • spec errors are not spamming full ex-data into the message

You can’t see it here but all compile errors also now carry ex-data which is available to tools.

1.8 is almost the same as 1.9 for these as there is only 1 spec example here.

2

u/final_fantasia Aug 11 '18 edited Aug 11 '18

Here are the equivalent error messages in Clojure 1.8. I'm glad to see Java exception class names removed from error messages.

Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14

user=> :::5
RuntimeException Invalid token: :::5  clojure.lang.Util.runtimeException (Util.java:221)

user=> (let [x])
IllegalArgumentException let requires an even number of forms in binding vector in user:1  clojure.core/let (core.clj:4333)

user=> (cond 1)
IllegalArgumentException cond requires an even number of forms  clojure.core/cond (core.clj:589)

user=> (defmulti 5 class)
ClassCastException java.lang.Long cannot be cast to clojure.lang.IObj  clojure.core/with-meta--4375 (core.clj:217)

user=> (def 5)
CompilerException java.lang.RuntimeException: First argument to def must be a Symbol, compiling:(/tmp/form-init3466436612016038338.clj:1:1) 

user=> (/ 1 0)
ArithmeticException Divide by zero  clojure.lang.Numbers.divide (Numbers.java:158)

user=> (assert false)
AssertionError Assert failed: false  user/eval1152 (form-init3466436612016038338.clj:1)

1

u/alexdmiller Aug 11 '18

Note that the Java exception names are still there for evaluation time errors but i had a hard time seeing value in them for other types.

3

u/final_fantasia Aug 11 '18

Hmm... That is where I was confused a little bit. The following is what's on the wiki page:

Evaluation error at clojure.lang.Numbers.divide (Numbers.java:163). __ArithmeticException__ Divide by zero

which does have the Java exception class name in it, which is different from what's in the screenshot you posted on Twitter, which doesn't.

Since these error messages are meant for human beings (instead of machines/tools), I personally think that Java exception class names are irrelevant in all of the categories of errors messages since the only two things I, as a human being, would like to know about an error are:

  1. What is wrong?

  2. Where is it?

So in this case I referenced, the specific error message "Divide by zero" already helps me answer question 1) so I don't see the value of the more generic exception class name (ArithmeticException) in this particular error message.

In general, I would personally prefer not to see anything Java-related (including Java exception class names) in Clojure error messages (the presence of Java-related stuff is, in my opinion, simply a leak of abstraction.) This is especially helpful for Clojure(Script) beginners who don't have any Java background, yet. I know this is difficult to achieve sometimes (e.g., java.lang.Long) and not in the scope of this particular effort but this would, IMHO, be the right direction in terms of improving error messages in general.

Anyway, it's exciting to see the core team responding to community feedback and addressing this particular issue. This is, for me, "the next big thing" in Clojure. Thank you very much!

2

u/alexdmiller Aug 12 '18

Oh, I may have a bug in the impl then - for evaluation errors, you should see the Java exception class in the message. Because these will flow out in Java interop and other cases there is often meaningful information in the cause exception class name (like ClassCastException), so the intent is that it will be there.

It is not a goal to remove Java-related stuff from the exception message. Clojure is a hosted language, embraces the host, and does not try to hide that fact. (It is a goal to remove stuff that's not useful, which may be either Java stuff or Clojure stuff.)

3

u/final_fantasia Aug 12 '18

Thank you for the explanation. I totally overlooked the cases where java.lang.Throwable.getMessage may return null and exception class names would be the only meaningful things in those cases.

1

u/the2bears Aug 11 '18

Why wouldn't you just run the expressions used with 1.8 and post the results?

5

u/alexdmiller Aug 11 '18

Because I’m on a phone. They should all be the same other than the spec one afaik.

5

u/the2bears Aug 11 '18

/u/alexdmiller, my comment was not a reply to you, rather to /u/clj_user. They'd "love" to see the results, but not near enough to actually take 5 minutes to view them in a REPL.

3

u/alexdmiller Aug 12 '18

Ah, sorry I misread that

3

u/alexdmiller Aug 11 '18 edited Aug 11 '18

I assume the point being made here is that in 1.8 there was no spec and you instead got the hand-crafted macro-specific message about "cond requires an even number of forms". That's certainly a better message than the spec error in 1.9 or 1.10, so point taken. But it's worth unpacking why and the bigger picture.

The cond form (and other complex macros like defn, ns, etc) in core have years of pounding and tons of custom code to explicitly check for common user errors and respond appropriately (it's like writing a mini compiler). The whole point of spec is to write the spec and automatically get those checks with automated messages. The specs can then automatically catch all sorts of cases that either were silently ignored in the past or failed in strange ways with confusing errors. So while this particular case maybe is not as good, I'll still contend that the spec is doing a better job checking much more thoroughly with no macro checking code required.

In this particular case, the bindings involved use destructuring and destructuring is a recursive spec that is inherently challenging to spec and report well automatically. That said, some simple tweaks to the specs can actually encode this check more directly (https://dev.clojure.org/jira/browse/CLJ-2376) and then with the 1.10 changes you might see something like this:

user> (let [x])
Syntax error macroexpanding clojure.core/let at (69:1). Cause: Call to clojure.core/let did not conform to spec.
In: [0] val: [x] fails spec:
:clojure.core.specs.alpha/bindings at: [:args :bindings] predicate: (even? (count %))

which I think is reasonable for an automated error.