Skip to content

Commit

Permalink
feat(TRE-856): add error causes
Browse files Browse the repository at this point in the history
  • Loading branch information
armed committed Oct 24, 2022
1 parent 76e9009 commit 40d90c2
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/k16/gx/beta/errors.cljc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(ns k16.gx.beta.errors)

(defrecord ErrorContext [error-type node-key node-contents signal-key])
(defrecord ErrorContext [error-type node-key
node-contents signal-key
causes])

(def ^:dynamic *err-ctx*
"Error context is used for creating/throwing exceptions with contextual data"
(map->ErrorContext {:error-type :general}))
(map->ErrorContext {:error-type :general :causes []}))

(defn gather-error-messages
[ex]
Expand All @@ -23,6 +25,12 @@

:else ex)))

(defn add-err-cause
"Adds cause to error context, evaluates to nil"
[cause]
(set! *err-ctx* (update *err-ctx* :causes conj cause))
nil)

(defn gx-err-data
([internal-data]
(gx-err-data nil internal-data))
Expand Down Expand Up @@ -65,14 +73,21 @@
(flatten)
(apply str)))

(defn- cause->str
[{:keys [title data exception]}]
(str "cause(" title " = " data "): " (gather-error-messages exception)))

(defn humanize-error
[{:keys [node-key signal-key message]} & rest-of-error]
[{:keys [node-key signal-key message causes]} & rest-of-error]
(let [rest-of-error (filter seq rest-of-error)]
(apply str (concat [(or message "Error") ": "
(tokenize "node = " node-key
"signal = " signal-key)]
(when (seq rest-of-error)
(conj (interpose "\n\t" rest-of-error)
"\n\t"))
(when (seq causes)
(conj (interpose "\n\t" (map cause->str causes))
"\n\t"))))))

(defmulti humanize :error-type)
Expand Down

0 comments on commit 40d90c2

Please sign in to comment.