Add log-ex middleware.

This commit is contained in:
mike-thompson-day8 2015-03-14 09:47:16 +11:00
parent 13058912ed
commit a5b1ccdb16
2 changed files with 20 additions and 9 deletions

View File

@ -65,13 +65,5 @@
handler-fn (lookup-handler event-id)]
(if (nil? handler-fn)
(warn "re-frame: no event handler registered for: \"" event-id "\". Ignoring.") ;; TODO: make exception
(try
(handler-fn app-db event-v)
(catch :default e
(do
;; use of a core.async loop seems to completely ruin exception stacks.
;; So we're going print the exception to the console here, before it gets trashed.
(.error js/console e.stack)
(throw e)))))))
(handler-fn app-db event-v))))

View File

@ -35,6 +35,25 @@
(if-not (identical? db new-db)
(reset! app-db new-db)))))))
(defn log-ex
"Middleware which catches and prints any handler-generated exceptions to console.
Handlers are called from within a core.async go-loop, and core.async produces
a special kind of hell when in comes to stacktraces. By the time an exception
has passed through a go-loop its stack is mangled beyond repair and you'll
have no idea where the exception was thrown.
So this middleware catches and prints to stacktrace before the core.async sausage
machine has done its work.
"
[handler]
(fn log-ex-handler
[db v]
(try
(handler db v)
(catch :default e ;; ooops, handler threw
(do
(.error js/console e.stack)
(throw e))))))
(defn debug
"Middleware which logs debug information to js/console for each event.