r/Clojure • u/xtreak • Aug 11 '18
Error message improvements in Clojure 1.10
https://mobile.twitter.com/puredanger/status/102810365424144384010
u/keymone Aug 11 '18
it's an improvement, but is something changing about how stack traces are printed? can i set some *var* to make it less verbose and focus more on my stack rather than java+clojure stacks?
2
u/alexdmiller Aug 11 '18
I haven't done much yet with stack traces but considering some things (in particular with a way to focus on the cause stack rather than the whole chain). Clojure is inherently compiled to bytecode and implemented in Java. I don't think it's generally possible to have a Clojure-only stack trace (and a lot of times the actual error flows out of the Java parts of Clojure).
4
u/keymone Aug 11 '18
i see, thanks for your work, every improvement is very much appreciated!
14
u/wherethebuffaloroam Aug 11 '18
In CIDER you can hit
pin the stacktrace popup to filter down to "Project only" stack frames. This option will stay on allowing you to quickly see just your part and then only a keypress away to see the full stack trace if you need to investigate further.13
u/alexdmiller Aug 11 '18
I think this a good example where a tool has more context and can help you focus on whats more important.
8
Aug 11 '18
I'm so glad this is being worked on. When I try to convince people to start using Clojure, the error messages are almost always the reason they don't want to mess with the language. Thanks for all your hard work to make these improvements. I'm looking forward to seeing more!
6
u/Eno6ohng Aug 11 '18
I'll just throw it in here: https://github.com/always-be-clojuring/issues/issues/7
Are there any plans to implement something like TS-XXXX approach described there? It'd open lots of cool possibilities for the tooling to evolve, and the "easy to google" argument is also a strong one. Also, I feel like it fits clojure's philosophy nicely.
1
u/alexdmiller Aug 11 '18
No plans, and I think locking stuff like that in codes is actually antithetical to Clojure's philosophy of making things amenable to evolution.
2
u/Eno6ohng Aug 11 '18
Hmm, I don't think I understand, sorry.
What I meant by "fits clojure's philosophy" is the fact that
it decouples human-readable message from the specific type and info about the error (e.g. instead of string "ClassCastException: can't cast from X to Y" that'd be edn: {:type :clojure.errors/cast, :from X, :to Y} - something like that);
it allows to add new errors and change the reporting format without introducing breaking changes (growth, not breakage).
You mean that errors should not be a part of the contract, so they can change and evolve and be not locked up by backwards compatibility?
2
u/alexdmiller Aug 11 '18
You mean that errors should not be a part of the contract, so they can change and evolve and be not locked up by backwards compatibility?
That. Trying to catalog the universe of possible Clojure errors and maintain that with compatibility over time does not seem like a good use of anyone's time. I think it's a dubious claim that this would actually be useful to tools or as an entry to alternate docs either.
3
u/Eno6ohng Aug 12 '18
Well, I would disagree (errors always were the main complaint + maria.cloud does a great job at improving the experience, but the approach they took could only get us so far; clojuredocs for errors (with examples etc) would be awesome imo), but I can definitely see your point. Thanks!
4
u/didibus Aug 11 '18
One more question, you mention ex-data is now part of all exceptions? Can you clarify what that means?
What will the ex-data map look like? Does it mean we can start relying on ex-data to identify the exact kind of exceptions for all of core? Will ex-data have a spec?
Thanks
2
u/alexdmiller Aug 11 '18
One more question, you mention ex-data is now part of all exceptions?
Not all, but CompilerException wrappers now implement IExceptionInfo and have ex-data. These are the important ones to convey the location in the user's source being read/macroexpanded/compiled. Runtime errors will just flow as is, so it's up to you to throw ex-info where you want to.
What will the ex-data map look like?
Here's a wip example, won't commit to any of this yet:
{:clojure.error/line 69, :clojure.error/column 1, :clojure.error/phase :clojure.phase/spec, :clojure.error/source "NO_SOURCE_PATH", :clojure.error/symbol clojure.core/let}Does it mean we can start relying on ex-data to identify the exact kind of exceptions for all of core?
No? Not sure what you mean though.
Will ex-data have a spec?
Not sure if you mean the function (probably not worth it) or the attributes, which we could do but not sure it's buying you much:
(s/def :clojure.error/line int?) (s/def :clojure.error/column int?) (s/def :clojure.error/phase (s/nilable qualified-keyword?)) (s/def :clojure.error/source (s/nilable string?)) (s/def :clojure.error/symbol (s/nilable symbol?)) etc
9
2
u/Aredington Aug 11 '18 edited Jun 18 '23
This comment removed in protest of Reddit's API changes. See https://www.theverge.com/2023/6/5/23749188/reddit-subreddit-private-protest-api-changes-apollo-charges. All comments from this account were so deleted, as of June 18, 2023. If you see any other comments from this account, it is due to malfeasance on the part of Reddit. -- mass edited with https://redact.dev/
2
u/halgari Aug 15 '18
Kind of sad when such a “amazing improvement” is so subtle that it has to be explained. I had to stare at the examples side-by-side before I could see the difference.
0
u/clj_user Aug 13 '18
Right, so this whole hubbub is about going from "throw RuntimeException ..." to "throw ArithmeticException..."
2
u/Aredington Aug 13 '18 edited Jun 18 '23
This comment removed in protest of Reddit's API changes. See https://www.theverge.com/2023/6/5/23749188/reddit-subreddit-private-protest-api-changes-apollo-charges. All comments from this account were so deleted, as of June 18, 2023. If you see any other comments from this account, it is due to malfeasance on the part of Reddit. -- mass edited with https://redact.dev/
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 zerowhich 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:
What is wrong?
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.getMessagemay returnnulland 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
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.
2
Aug 12 '18
I feel like these are still pretty bad...
2
u/fernandohur Aug 13 '18
Instead of just saying it's bad, could you mention specific cases that don't match your expectations? this way the maintainers can learn something from the way users work with error messages and maybe even make an improvement for your particular case.
4
Aug 13 '18
Errors in Clojure are one of the most frustrating part of the language, especially for beginners. It's obvious that these error messages are an improvement, but in the sense that they're bringing it up to a baseline of bare minimum expectation for a modern language.
I'm highly disappointed by the claim that linking to error documentation would be brittle. For example, in Rust, even when the compiler barfs out garbage for an error that is difficult to understand, often there is at least a link to documentation that explains the error in a helpful manner.
1
u/halgari Aug 15 '18
Pretty much this. With these improvements Clojure’s errors will go from useless, to “meh”. There would need to be a lot of tooling and more improvements before these errors could be considered “good” or even on par with Python, Rust, Clang, Elm, etc.
1
u/jiyinyiyong Aug 12 '18
Would this affect ClojureScript?
2
u/alexdmiller Aug 12 '18
Not yet, although extending these principles to cljs is a logical next step once we finalize things.
-4
u/clj_user Aug 13 '18
So we've waited years for good error messages for almost a decade, and we get:
More distinct error types: Arithmetic/Compile/Syntax exceptions vs Runtime exceptions
Inclusion of more information around the error: symbol given, macro being defined
Suppression of gook dumped into the error by spec.
Fantastic! It's been 2 years since Spec was released, and almost 10 years since Clojure 1.0. And we're finally getting features that any self-respecting language should include in the original release. And in the mean time clojure developers have had to rationalize away why these errors are so bad:
https://lispcast.com/clojure-error-messages-accidental/
https://www.youtube.com/watch?v=FihU5JxmnBg
http://blog.cognitect.com/blog/2017/6/5/repl-debugging-no-stacktrace-required
What frustrates me is being told that Clojure doesn't need good error messages, because "garbage in, garbage out", or REPL driven development means we don't need good stack traces. And now it all matters because the core devs care now.
Too little, too late. From what I can tell, those who would be impacted by this have already left the language.
13
u/didibus Aug 11 '18
Looks awesome. My only feeling is, I think I'd prefer the cause to be first?
Like Divide by zero. Evaluation error at bla bla.
Instead of: Evaluation error at bla bla. Divide by zero.
Or: Cond requires an even number of form. Syntax error macroexpending cond at (3:1).
I feel the cause is what helps me most fix the issue, and so seeing it first seems better to me.