improve re-frame db spec report

This commit is contained in:
pablodip 2017-12-20 10:57:46 +07:00 committed by Julien Eluard
parent 9ca54e9adc
commit b28765c38a
1 changed files with 38 additions and 3 deletions

View File

@ -29,6 +29,40 @@
(log/debug "Handling re-frame event: " (first (get-coeffect context :event))) (log/debug "Handling re-frame event: " (first (get-coeffect context :event)))
context))) context)))
(defn- check-spec-msg-path-problem [problem]
(let [pred (:pred problem)]
(str "Spec: " (-> problem :via last) "\n"
"Predicate: " (subs (str (:pred problem)) 0 50))))
(defn- check-spec-msg-path-problems [path path-problems]
(str "Key path: " path "\n"
"Val: " (pr-str (-> path-problems first :val)) "\n\n"
"Number of problems: " (count path-problems) "\n\n"
(->> path-problems
(map check-spec-msg-path-problem)
(interpose "\n\n")
(apply str))))
(defn- check-spec-msg [event-id db]
(let [explain-data (spec/explain-data :status-im.ui.screens.db/db db)
problems (::spec/problems explain-data)
db-root-keys (->> problems (map (comp first :in)) set)
heading #(str "\n\n------\n" % "\n------\n\n")
msg (str "re-frame db spec check failed."
(heading "SUMMARY")
"After event id: " event-id "\n"
"Number of problems: " (count problems) "\n\n"
"Failing root db keys:\n"
(->> db-root-keys (interpose "\n") (apply str))
(heading "PROBLEMS")
(->> problems
(group-by :in)
(map (fn [[path path-problems]]
(check-spec-msg-path-problems path path-problems)))
(interpose "\n\n-----\n\n")
(apply str)))]
msg))
(def check-spec (def check-spec
"throw an exception if db doesn't match the spec" "throw an exception if db doesn't match the spec"
(->interceptor (->interceptor
@ -37,11 +71,12 @@
(fn check-handler (fn check-handler
[context] [context]
(let [new-db (get-effect context :db) (let [new-db (get-effect context :db)
v (get-coeffect context :event)] event-id (-> (get-coeffect context :event) first)]
(when (and new-db (not (spec/valid? :status-im.ui.screens.db/db new-db))) (when (and new-db (not (spec/valid? :status-im.ui.screens.db/db new-db)))
(throw (ex-info (str "spec check failed on: " (first v) "\n " (spec/explain-str :status-im.ui.screens.db/db new-db)) {}))) (throw (ex-info (check-spec-msg event-id new-db) {})))
context)))) context))))
(defn register-handler (defn register-handler
([name handler] (register-handler name nil handler)) ([name handler] (register-handler name nil handler))
([name middleware handler] ([name middleware handler]