Skip to content

Commit

Permalink
Fix code error ui integration
Browse files Browse the repository at this point in the history
This commit adds some tests for error handling in the code evaluation
flow. It also fixes the broken ui integration with code errors.
  • Loading branch information
neenaoffline committed Nov 21, 2023
1 parent 5ffcf77 commit 21ae2b5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
2 changes: 2 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ a {
line-height: var(--cell-height);
color: var(--sheet-error);
margin-left: 10px;
overflow: hidden;
/* This is quite useless, but cleaner than overflowing */
}

.code-header-btn {
Expand Down
2 changes: 1 addition & 1 deletion src/bean/code.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require [bean.grid :as grid]))

(defn reevaluate [{:keys [code-in-editor] :as sheet}]
(grid/eval-code sheet code-in-editor))
(grid/eval-code sheet code-in-editor false))

(defn set-code [sheet code]
(assoc sheet :code-in-editor code))
Expand Down
3 changes: 3 additions & 0 deletions src/bean/code_errors.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(ns bean.code-errors)

(defn get-error [sheet]
(:code-error sheet))

(defn named-ref-error [named error]
(str "name: " named ". " error))
22 changes: 13 additions & 9 deletions src/bean/grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,15 @@
:depgraph (deps/make-depgraph parsed-grid)}))

(defn- escalate-bindings-errors [sheet]
(reduce (fn [sheet [named {:keys [error]}]]
(if error
(reduced (assoc sheet :code-error (code-errors/named-ref-error named error)))
sheet))
(dissoc sheet :code-error)
(:bindings sheet)))
(if (code-errors/get-error sheet)
;; This likely already set a parse error, return as is
sheet
(reduce (fn [sheet [named {:keys [error]}]]
(if error
(reduced (assoc sheet :code-error (code-errors/named-ref-error named error)))
sheet))
(dissoc sheet :code-error)
(:bindings sheet))))

(declare eval-dep)

Expand Down Expand Up @@ -222,9 +225,10 @@
(defn eval-code
;; Suppressing errors so we let the grid evaluate before showing any errors in the code
([sheet] (eval-code sheet (:code sheet) true))
([sheet code & suppress-errors]
(let [res (let [code-ast (parser/parse-statement code)]
(if-let [parse-error (parser/error code-ast)]
([sheet code suppress-errors]
(let [res (let [code-ast (parser/parse-statement code)
parse-error (parser/error code-ast)]
(if (string? parse-error)
(assoc sheet :code-error parse-error)
(-> (reduce (fn [sheet [_ [_ named] expr]]
(eval-named named
Expand Down
5 changes: 3 additions & 2 deletions src/bean/ui/code.cljs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns bean.ui.code
(:require [bean.code :as code]
[bean.ui.util :refer [cs]]))
[bean.ui.util :refer [cs]]
[bean.code-errors :as code-errors]))

(defn set-eval-state [sheet]
(assoc-in sheet
[:ui :code-evaluation-state]
(if (:errors sheet)
(if (code-errors/get-error sheet)
:error
:evaluated)))

Expand Down
26 changes: 26 additions & 0 deletions test/bean/code_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns bean.code-test
(:require [clojure.test :refer [deftest testing is]]
[bean.code :as code]
[bean.grid :as grid]))

(deftest code-errors-test
(testing "When the code has a parse error, it is escalated on reevaluation"
(is (= (-> (grid/new-sheet [[""]] "")
(code/set-code "foo: 99fail")
code/reevaluate
:code-error)
"Parse Error: idx 7. [{:tag :string, :expecting \"*\"} {:tag :regexp, :expecting #\"^\\s+\"} {:tag :string, :expecting \"+\"} {:tag :string, :expecting \"\\n\"}]")))

(testing "When the code has an error in a statement, it is escalated on reevaluation"
(is (= (-> (grid/new-sheet [[""]] "")
(code/set-code "foo: B3")
code/reevaluate
:code-error)
"name: foo. Invalid address [2 1]")))

(testing "When the code has an error in a statement, it is escalated on initial
evaluation after the grid is evaluated"
(is (= (-> (grid/new-sheet [["1"]] "foo: A1+\"bar\"")
grid/eval-sheet
:code-error)
"name: foo. + only works for Integers"))))
2 changes: 1 addition & 1 deletion test/bean/grid_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
(is (= {[:cell [0 1]] #{[:named "addaone"]}}
(as-> (new-sheet [["1" "2"]] "addaone:4+A1") sheet
(eval-sheet sheet)
(eval-code sheet "addaone:4+B1")
(eval-code sheet "addaone:4+B1" false)
(get-in sheet [:depgraph])))))

(testing "Depgraph is updated when a named reference's dependents change"
Expand Down

0 comments on commit 21ae2b5

Please sign in to comment.