mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 16:31:11 +00:00
Merge pull request #274 from metosin/route-data-merge-error
Better errors for route-data merge errors
This commit is contained in:
commit
4831a86310
4 changed files with 44 additions and 6 deletions
|
|
@ -7,11 +7,14 @@
|
||||||
([type data]
|
([type data]
|
||||||
(throw (ex-info (str type) {:type type, :data data}))))
|
(throw (ex-info (str type) {:type type, :data data}))))
|
||||||
|
|
||||||
|
(defn get-message [e]
|
||||||
|
#?(:clj (.getMessage ^Exception e) :cljs (ex-message e)))
|
||||||
|
|
||||||
(defmulti format-exception (fn [type _ _] type))
|
(defmulti format-exception (fn [type _ _] type))
|
||||||
|
|
||||||
(defn exception [e]
|
(defn exception [e]
|
||||||
(let [data (ex-data e)
|
(let [data (ex-data e)
|
||||||
message (format-exception (:type data) #?(:clj (.getMessage ^Exception e) :cljs (ex-message e)) (:data data))]
|
message (format-exception (:type data) (get-message e) (:data data))]
|
||||||
;; there is a 3-arity version (+cause) of ex-info, but the default repl error message is taken from the cause
|
;; there is a 3-arity version (+cause) of ex-info, but the default repl error message is taken from the cause
|
||||||
(ex-info message (assoc (or data {}) ::cause e))))
|
(ex-info message (assoc (or data {}) ::cause e))))
|
||||||
|
|
||||||
|
|
@ -35,3 +38,6 @@
|
||||||
(fn [[name vals]]
|
(fn [[name vals]]
|
||||||
(str name "\n-> " (str/join "\n-> " (mapv first vals)) "\n"))
|
(str name "\n-> " (str/join "\n-> " (mapv first vals)) "\n"))
|
||||||
conflicts)))
|
conflicts)))
|
||||||
|
|
||||||
|
(defmethod format-exception :reitit.impl/merge-data [_ _ data]
|
||||||
|
(str "Error merging route-data\n\n" (pr-str data)))
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@
|
||||||
[clojure.set :as set]
|
[clojure.set :as set]
|
||||||
[meta-merge.core :as mm]
|
[meta-merge.core :as mm]
|
||||||
[reitit.trie :as trie]
|
[reitit.trie :as trie]
|
||||||
[reitit.exception :as exception])
|
[reitit.exception :as exception]
|
||||||
|
[reitit.exception :as ex])
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(:import (java.util.regex Pattern)
|
(:import (java.util.regex Pattern)
|
||||||
(java.util HashMap Map)
|
(java.util HashMap Map)
|
||||||
|
|
@ -58,12 +59,15 @@
|
||||||
(walk-one path (mapv identity data) raw-routes)))
|
(walk-one path (mapv identity data) raw-routes)))
|
||||||
|
|
||||||
(defn map-data [f routes]
|
(defn map-data [f routes]
|
||||||
(mapv #(update % 1 f) routes))
|
(mapv (fn [[p ds]] [p (f p ds)]) routes))
|
||||||
|
|
||||||
(defn merge-data [x]
|
(defn merge-data [p x]
|
||||||
(reduce
|
(reduce
|
||||||
(fn [acc [k v]]
|
(fn [acc [k v]]
|
||||||
(mm/meta-merge acc {k v}))
|
(try
|
||||||
|
(mm/meta-merge acc {k v})
|
||||||
|
(catch #?(:clj Exception, :cljs js/Error) e
|
||||||
|
(ex/fail! ::merge-data {:path p, :left acc, :right {k v}, :exception e}))))
|
||||||
{} x))
|
{} x))
|
||||||
|
|
||||||
(defn resolve-routes [raw-routes {:keys [coerce] :as opts}]
|
(defn resolve-routes [raw-routes {:keys [coerce] :as opts}]
|
||||||
|
|
|
||||||
|
|
@ -335,3 +335,27 @@
|
||||||
problems))
|
problems))
|
||||||
(color :white "https://cljdoc.org/d/metosin/reitit/CURRENT/doc/basics/route-data-validation")
|
(color :white "https://cljdoc.org/d/metosin/reitit/CURRENT/doc/basics/route-data-validation")
|
||||||
[:break]])
|
[:break]])
|
||||||
|
|
||||||
|
(defmethod format-exception :reitit.impl/merge-data [_ _ {:keys [path left right exception]}]
|
||||||
|
[:group
|
||||||
|
(text "Error merging route-data:")
|
||||||
|
[:break] [:break]
|
||||||
|
[:group
|
||||||
|
[:span (color :grey "-- On route -----------------------")]
|
||||||
|
[:break]
|
||||||
|
[:break]
|
||||||
|
(text path)
|
||||||
|
[:break]
|
||||||
|
[:break]
|
||||||
|
[:span (color :grey "-- Exception ----------------------")]
|
||||||
|
[:break]
|
||||||
|
[:break]
|
||||||
|
(color :red (exception/get-message exception))
|
||||||
|
[:break]
|
||||||
|
[:break]
|
||||||
|
(edn left {:margin 3})
|
||||||
|
[:break]
|
||||||
|
(edn right {:margin 3})]
|
||||||
|
[:break]
|
||||||
|
(color :white "https://cljdoc.org/d/metosin/reitit/CURRENT/doc/basics/route-data")
|
||||||
|
[:break]])
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,11 @@
|
||||||
["/api/{ipa"]
|
["/api/{ipa"]
|
||||||
|
|
||||||
#"Invalid route data"
|
#"Invalid route data"
|
||||||
["/api/ipa" {::roles #{:adminz}}])
|
["/api/ipa" {::roles #{:adminz}}]
|
||||||
|
|
||||||
|
#"Error merging route-data"
|
||||||
|
["/a" {:body {}}
|
||||||
|
["/b" {:body [:FAIL]}]])
|
||||||
|
|
||||||
exception/exception
|
exception/exception
|
||||||
pretty/exception))
|
pretty/exception))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue