Skip to content

Commit

Permalink
Merge pull request metosin#274 from metosin/route-data-merge-error
Browse files Browse the repository at this point in the history
Better errors for route-data merge errors
  • Loading branch information
ikitommi authored May 16, 2019
2 parents 9a75219 + 639b0ca commit 4831a86
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
8 changes: 7 additions & 1 deletion modules/reitit-core/src/reitit/exception.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
([type 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))

(defn exception [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
(ex-info message (assoc (or data {}) ::cause e))))

Expand All @@ -35,3 +38,6 @@
(fn [[name vals]]
(str name "\n-> " (str/join "\n-> " (mapv first vals)) "\n"))
conflicts)))

(defmethod format-exception :reitit.impl/merge-data [_ _ data]
(str "Error merging route-data\n\n" (pr-str data)))
12 changes: 8 additions & 4 deletions modules/reitit-core/src/reitit/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[clojure.set :as set]
[meta-merge.core :as mm]
[reitit.trie :as trie]
[reitit.exception :as exception])
[reitit.exception :as exception]
[reitit.exception :as ex])
#?(:clj
(:import (java.util.regex Pattern)
(java.util HashMap Map)
Expand Down Expand Up @@ -58,12 +59,15 @@
(walk-one path (mapv identity data) raw-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
(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))

(defn resolve-routes [raw-routes {:keys [coerce] :as opts}]
Expand Down
24 changes: 24 additions & 0 deletions modules/reitit-dev/src/reitit/dev/pretty.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,27 @@
problems))
(color :white "https://cljdoc.org/d/metosin/reitit/CURRENT/doc/basics/route-data-validation")
[: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]])
6 changes: 5 additions & 1 deletion test/cljc/reitit/exception_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
["/api/{ipa"]

#"Invalid route data"
["/api/ipa" {::roles #{:adminz}}])
["/api/ipa" {::roles #{:adminz}}]

#"Error merging route-data"
["/a" {:body {}}
["/b" {:body [:FAIL]}]])

exception/exception
pretty/exception))

0 comments on commit 4831a86

Please sign in to comment.