Improve internal errors.

This commit is contained in:
Mike Thompson 2017-07-22 10:04:03 +10:00
parent 8d13634c55
commit dc26343727
1 changed files with 9 additions and 8 deletions

View File

@ -14,21 +14,21 @@
(defn- flatten-and-remove-nils
"`interceptors` might have nested collections, and contain nil elements.
return a flat collection, with all nils removed.
This function is 9/10 about giving good error messages"
This function is 9/10 about giving good error messages."
[id interceptors]
(let [make-chain #(->> % flatten (remove nil?))]
(if-not debug-enabled?
(make-chain interceptors)
(do ;; do a whole lot of development time checks
(when-not (coll? interceptors)
(console :error (str "re-frame: when registering " id ", expected a collection of interceptors, got:") interceptors))
(console :error "re-frame: when registering " id ", expected a collection of interceptors, got: " interceptors))
(let [chain (make-chain interceptors)]
(when (empty? chain)
(console :error (str "re-frame: when registering" id ", given an empty interceptor chain")))
(console :error "re-frame: when registering " id ", given an empty interceptor chain"))
(when-let [not-i (first (remove interceptor/interceptor? chain))]
(if (fn? not-i)
(console :error (str "re-frame: when registering " id ", got a function instead of an interceptor. Did you provide old style middleware by mistake? Got:") not-i)
(console :error (str "re-frame: when registering " id ", expected interceptors, but got:") not-i)))
(console :error "re-frame: when registering " id ", got a function instead of an interceptor. Did you provide old style middleware by mistake? Got: " not-i)
(console :error "re-frame: when registering " id ", expected interceptors, but got: " not-i)))
chain)))))
@ -39,7 +39,8 @@
at any level,so process this structure into a simple, nil-less vector
before registration.
An `event handler` will likely be at the end of the chain (wrapped in an interceptor)."
Typically, an `event handler` will be at the end of the chain (wrapped
in an interceptor)."
[id interceptors]
(register-handler kind id (flatten-and-remove-nils id interceptors)))
@ -50,12 +51,12 @@
(def ^:dynamic *handling* nil) ;; remember what event we are currently handling
(defn handle
"Given an event vector, look up the associated intercepter chain, and execute it."
"Given an event vector `event-v`, look up the associated interceptor chain, and execute it."
[event-v]
(let [event-id (first-in-vector event-v)]
(if-let [interceptors (get-handler kind event-id true)]
(if *handling*
(console :error (str "re-frame: while handling \"" *handling* "\", dispatch-sync was called for \"" event-v "\". You can't call dispatch-sync within an event handler."))
(console :error "re-frame: while handling \"" *handling* "\", dispatch-sync was called for \"" event-v "\". You can't call dispatch-sync within an event handler.")
(binding [*handling* event-v]
(trace/with-trace {:operation event-id
:op-type kind