Skip to content

Commit

Permalink
Inform users with a default event error handler
Browse files Browse the repository at this point in the history
Relevant to: #231 #423 #627
  • Loading branch information
kimo-k committed Nov 3, 2023
1 parent fe68b6b commit 3e9b6d2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/re_frame/alpha.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@
[handler]
(registrar/register-handler :error :event-handler handler))

(reg-event-error-handler interceptor/default-error-handler)

;; -- subscriptions -----------------------------------------------------------

(defn sub
Expand Down
2 changes: 2 additions & 0 deletions src/re_frame/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@
[handler]
(registrar/register-handler :error :event-handler handler))

(reg-event-error-handler interceptor/default-error-handler)

;; -- interceptors ------------------------------------------------------------

(def ^{:api-docs/heading "Interceptors"} debug
Expand Down
26 changes: 19 additions & 7 deletions src/re_frame/interceptor.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[re-frame.interop :refer [empty-queue debug-enabled?]]
[re-frame.trace :as trace :include-macros true]
[re-frame.registrar :as registrar]
[re-frame.utils :as u]
[clojure.set :as set]))

(def mandatory-interceptor-keys #{:id :after :before})
Expand Down Expand Up @@ -165,6 +166,19 @@
(apply merge (ex-data e) ms)
#?(:clj (.getCause e) :cljs (ex-cause e))))

(defn default-error-handler [original-error re-frame-error]
(let [{:keys [event-v direction interceptor]} (ex-data re-frame-error)
event-handler? (#{:db-handler :fx-handler :ctx-handler} interceptor)]
(apply console :error
"An error occured while handling the re-frame event:"
(str event-v)
"\n"
(map str
(if event-handler?
["Within the" (first event-v) "event handler function."]
["Within the" direction "phase of the" (pr-str interceptor) "interceptor."])))
(throw original-error)))

(defn execute
"Executes the given chain (coll) of interceptors.
Expand Down Expand Up @@ -219,10 +233,8 @@
error-handler (registrar/get-handler :error :event-handler)]
(trace/merge-trace!
{:tags {:interceptors interceptors}})
(if-not error-handler
(execute* ctx)
(try
(execute* (assoc ctx ::augment-exceptions? true))
(catch #?(:clj Exception :cljs :default) e
(error-handler (ex-cause e)
(merge-ex-data e {:event-v event-v})))))))
(try
(execute* (assoc ctx ::augment-exceptions? true))
(catch #?(:clj Exception :cljs :default) e
(error-handler (ex-cause e)
(merge-ex-data e {:event-v event-v}))))))

0 comments on commit 3e9b6d2

Please sign in to comment.