mirror of
https://github.com/metosin/reitit.git
synced 2026-02-11 22:05:16 +00:00
Better error messages
This commit is contained in:
parent
066f5752c2
commit
bf0d327570
2 changed files with 16 additions and 9 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
(ns reitit.core
|
(ns reitit.core
|
||||||
(:require [meta-merge.core :refer [meta-merge]]
|
(:require [meta-merge.core :refer [meta-merge]]
|
||||||
|
[clojure.string :as str]
|
||||||
[reitit.impl :as impl #?@(:cljs [:refer [Route]])])
|
[reitit.impl :as impl #?@(:cljs [:refer [Route]])])
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(:import (reitit.impl Route))))
|
(:import (reitit.impl Route))))
|
||||||
|
|
@ -74,7 +75,11 @@
|
||||||
(defn throw-on-conflicts! [conflicts]
|
(defn throw-on-conflicts! [conflicts]
|
||||||
(throw
|
(throw
|
||||||
(ex-info
|
(ex-info
|
||||||
(str "router contains conflicting routes: " conflicts)
|
(apply str "router contains conflicting routes:\n\n"
|
||||||
|
(mapv
|
||||||
|
(fn [[[path] vals]]
|
||||||
|
(str " " path "\n-> " (str/join "\n-> " (mapv first vals)) "\n\n"))
|
||||||
|
conflicts))
|
||||||
{:conflicts conflicts})))
|
{:conflicts conflicts})))
|
||||||
|
|
||||||
(defn name-lookup [[_ {:keys [name]}] opts]
|
(defn name-lookup [[_ {:keys [name]}] opts]
|
||||||
|
|
@ -118,7 +123,7 @@
|
||||||
:conflicts throw-on-conflicts!})
|
:conflicts throw-on-conflicts!})
|
||||||
|
|
||||||
(defn linear-router
|
(defn linear-router
|
||||||
"Creates a [[LinearRouter]] from resolved routes and optional
|
"Creates a LinearRouter from resolved routes and optional
|
||||||
expanded options. See [[router]] for available options"
|
expanded options. See [[router]] for available options"
|
||||||
([routes]
|
([routes]
|
||||||
(linear-router routes {}))
|
(linear-router routes {}))
|
||||||
|
|
@ -164,11 +169,11 @@
|
||||||
([routes]
|
([routes]
|
||||||
(lookup-router routes {}))
|
(lookup-router routes {}))
|
||||||
([routes opts]
|
([routes opts]
|
||||||
(when-let [route (some impl/contains-wilds? (map first routes))]
|
(when-let [wilds (seq (filter impl/wild-route? routes))]
|
||||||
(throw
|
(throw
|
||||||
(ex-info
|
(ex-info
|
||||||
(str "can't create LookupRouter with wildcard routes: " route)
|
(str "can't create LookupRouter with wildcard routes: " wilds)
|
||||||
{:route route
|
{:wilds wilds
|
||||||
:routes routes})))
|
:routes routes})))
|
||||||
(let [compiled (map #(compile-route % opts) routes)
|
(let [compiled (map #(compile-route % opts) routes)
|
||||||
names (find-names routes opts)
|
names (find-names routes opts)
|
||||||
|
|
@ -218,7 +223,6 @@
|
||||||
(let [opts (meta-merge default-router-options opts)
|
(let [opts (meta-merge default-router-options opts)
|
||||||
routes (resolve-routes data opts)]
|
routes (resolve-routes data opts)]
|
||||||
(when-let [conflicts (:conflicts opts)]
|
(when-let [conflicts (:conflicts opts)]
|
||||||
(when-let [conflicting (conflicting-routes routes)]
|
(when conflicting (conflicts conflicting)))
|
||||||
(conflicts conflicting)))
|
|
||||||
((if (some impl/contains-wilds? (map first routes))
|
(router routes opts))))
|
||||||
linear-router lookup-router) routes opts))))
|
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,9 @@
|
||||||
(defn- catch-all? [segment]
|
(defn- catch-all? [segment]
|
||||||
(= \* (first segment)))
|
(= \* (first segment)))
|
||||||
|
|
||||||
|
(defn wild-route? [[path]]
|
||||||
|
(contains-wilds? path))
|
||||||
|
|
||||||
(defn conflicting-routes? [[p1 :as route1] [p2 :as route2]]
|
(defn conflicting-routes? [[p1 :as route1] [p2 :as route2]]
|
||||||
(loop [[s1 & ss1] (segments p1)
|
(loop [[s1 & ss1] (segments p1)
|
||||||
[s2 & ss2] (segments p2)]
|
[s2 & ss2] (segments p2)]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue