planck-repl / planck

Stand-alone ClojureScript REPL
https://planck-repl.org
Eclipse Public License 1.0
1.03k stars 67 forks source link

Imitate clojure.main Error Reporting #1004

Open lambrospetrou opened 4 years ago

lambrospetrou commented 4 years ago

I was writing a small function to traverse a binary tree and due to an extra parentheses somewhere in the code I kept getting the following error:

Execution error (Error) at (<cljs repl>:1).
Invalid arity: 0

Without any hint on the line or in the macro/function that the erroneous function call happened it was very hard to find the mistake. I eventually did find it after a few hours but it would be great if the error message was more descriptive and could include some more details.

Unfortunately, I don't have experience with the ClojureScript compile or internal implementation of the macros so I don't really know if there even exists any information during runtime that could be useful but I thought it would be great to expose it if there was.

The code throwing this error is below:

; `repro.cljs`
(ns solutions.daily-coding-problem.repro)

(defn deepest-node [root]
  (defn do-deep-rec [n, depth]
    (cond
      (nil? n) [nil 0]
      (and (nil? (:l n)) (nil? (:r n))) [n depth]
      :else (
        (let [
          [ln ld] (do-deep-rec (:l n) (inc depth))
          [rn rd] (do-deep-rec (:r n) (inc depth))]
          (if (> ld rd)
            [ln ld]
            [rn rd])))))
  (get (do-deep-rec root 1) 0))

(println (:val (deepest-node 
  {
    :val "a",
    :l {
      :val "b"
      :l { :val "d" }
    },
    :r {:val "c"}
  })))

After saving the above into repro.cljs, running the code with plk repro.cljs will generate the error.

The problem is an extra pair of parentheses for the :else clause, so removing those fixes the problem and the code runs fine.

Thanks a lot.

mfikes commented 4 years ago

My initial hunch is that we should make this scenario behave like Clojure 1.10.1 (putting more details into a temp edn file that is referenced when the exception summary is printed).

Details: https://github.com/clojure/clojure/blob/8f03ff0078dda9fb3923ff0c4ee5a101570b8485/changes.md#12-clojuremain-error-reporting