mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
on-coercion-error
This commit is contained in:
parent
f0405adc02
commit
9e4b420fc8
3 changed files with 30 additions and 13 deletions
|
|
@ -23,17 +23,25 @@
|
||||||
(defn match-by-path
|
(defn match-by-path
|
||||||
"Given routing tree and current path, return match with possibly
|
"Given routing tree and current path, return match with possibly
|
||||||
coerced parameters. Return nil if no match found."
|
coerced parameters. Return nil if no match found."
|
||||||
[router path]
|
([router path] (match-by-path router path nil))
|
||||||
(let [uri (.parse Uri path)]
|
([router path {:keys [on-coercion-error]}]
|
||||||
(if-let [match (r/match-by-path router (.getPath uri))]
|
(let [uri (.parse Uri path)
|
||||||
(let [q (query-params uri)
|
coerce! (if on-coercion-error
|
||||||
match (assoc match :query-params q)
|
(fn [match]
|
||||||
;; Return uncoerced values if coercion is not enabled - so
|
(try (coercion/coerce! match)
|
||||||
;; that tha parameters are always accessible from same property.
|
(catch js/Error e
|
||||||
parameters (or (coercion/coerce! match)
|
(on-coercion-error e)
|
||||||
{:path (:path-params match)
|
(throw e))))
|
||||||
:query q})]
|
coercion/coerce!)]
|
||||||
(assoc match :parameters parameters)))))
|
(if-let [match (r/match-by-path router (.getPath uri))]
|
||||||
|
(let [q (query-params uri)
|
||||||
|
match (assoc match :query-params q)
|
||||||
|
;; Return uncoerced values if coercion is not enabled - so
|
||||||
|
;; that tha parameters are always accessible from same property.
|
||||||
|
parameters (or (coerce! match)
|
||||||
|
{:path (:path-params match)
|
||||||
|
:query q})]
|
||||||
|
(assoc match :parameters parameters))))))
|
||||||
|
|
||||||
(defn match-by-name
|
(defn match-by-name
|
||||||
"Given a router, route name and optionally path-parameters,
|
"Given a router, route name and optionally path-parameters,
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@
|
||||||
nil)
|
nil)
|
||||||
(-on-navigate [this path]
|
(-on-navigate [this path]
|
||||||
(reset! last-fragment path)
|
(reset! last-fragment path)
|
||||||
(on-navigate (rf/match-by-path router path) this))
|
(on-navigate (rf/match-by-path router path this) this))
|
||||||
(-get-path [this]
|
(-get-path [this]
|
||||||
;; Remove #
|
;; Remove #
|
||||||
;; "" or "#" should be same as "#/"
|
;; "" or "#" should be same as "#/"
|
||||||
|
|
@ -125,7 +125,7 @@
|
||||||
(-on-navigate this (-get-path this))
|
(-on-navigate this (-get-path this))
|
||||||
this))
|
this))
|
||||||
(-on-navigate [this path]
|
(-on-navigate [this path]
|
||||||
(on-navigate (rf/match-by-path router path) this))
|
(on-navigate (rf/match-by-path router path this) this))
|
||||||
(-stop [this]
|
(-stop [this]
|
||||||
(gevents/unlistenByKey listen-key)
|
(gevents/unlistenByKey listen-key)
|
||||||
(gevents/unlistenByKey click-listen-key)
|
(gevents/unlistenByKey click-listen-key)
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,15 @@
|
||||||
(is (= "/5"
|
(is (= "/5"
|
||||||
(r/match->path (rf/match-by-name router ::foo {:id 5}))))
|
(r/match->path (rf/match-by-name router ::foo {:id 5}))))
|
||||||
|
|
||||||
|
(testing "coercion error"
|
||||||
|
(testing "throws without options"
|
||||||
|
(is (thrown? js/Error (m (rf/match-by-path router "/a")))))
|
||||||
|
|
||||||
|
(testing "thows and calles on-coercion-error"
|
||||||
|
(let [exception (atom nil)]
|
||||||
|
(is (thrown? js/Error (m (rf/match-by-path router "/a" {:on-coercion-error (fn [e] (reset! exception e))}))))
|
||||||
|
(is (= {:id "a"} (-> @exception (ex-data) :value))))))
|
||||||
|
|
||||||
(testing "query param is read"
|
(testing "query param is read"
|
||||||
(is (= (r/map->Match
|
(is (= (r/map->Match
|
||||||
{:template "/:id"
|
{:template "/:id"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue