Log warnings about missing route or params

This commit is contained in:
Juho Teperi 2018-07-11 12:39:10 +03:00
parent 358fbc19c6
commit ae109e5350
3 changed files with 29 additions and 3 deletions

View file

@ -17,7 +17,10 @@
(defn about-page []
[:div
[:h2 "About frontend"]
[:a {:href "http://google.com"} "external link"]])
[:ul
[:li [:a {:href "http://google.com"} "external link"]]
[:li [:a {:href (rfh/href @router ::foobar)} "Missing route"]]
[:li [:a {:href (rfh/href @router ::item)} "Missing route params"]]]])
(defn item-page [match]
(let [{:keys [path query]} (:parameters match)

View file

@ -2,6 +2,7 @@
""
(:require [reitit.core :as reitit]
[clojure.string :as str]
[clojure.set :as set]
[reitit.coercion :as coercion]
[goog.events :as e]
[goog.dom :as dom])
@ -38,3 +39,25 @@
(match-by-name router name {}))
([router name path-params]
(reitit/match-by-name router name path-params)))
(defn match-by-name!
"Logs problems using console.warn"
([router name]
(match-by-name! router name {}))
([router name path-params]
(if-let [match (reitit/match-by-name router name path-params)]
(if (reitit/partial-match? match)
(if (every? #(contains? path-params %) (:required match))
match
(let [defined (-> path-params keys set)
missing (set/difference (:required match) defined)]
(js/console.warn
"missing path-params for route" name
{:template (:template match)
:missing missing
:path-params path-params
:required (:required match)})
nil))
match)
(do (js/console.warn "missing route" name)
nil))))

View file

@ -110,7 +110,7 @@
([state k params]
(href state k params nil))
([{:keys [router history]} k params query]
(let [match (rf/match-by-name router k params)
(let [match (rf/match-by-name! router k params)
token (match->token history match k params query)]
(token->href history token))))
@ -118,6 +118,6 @@
([state k params]
(replace-token state k params nil))
([{:keys [router history]} k params query]
(let [match (rf/match-by-name router k params)
(let [match (rf/match-by-name! router k params)
token (match->token history match k params query)]
(.replaceToken history token))))