Better error messages

This commit is contained in:
Tommi Reiman 2017-08-22 08:27:34 +03:00
parent 066f5752c2
commit bf0d327570
2 changed files with 16 additions and 9 deletions

View file

@ -1,5 +1,6 @@
(ns reitit.core
(:require [meta-merge.core :refer [meta-merge]]
[clojure.string :as str]
[reitit.impl :as impl #?@(:cljs [:refer [Route]])])
#?(:clj
(:import (reitit.impl Route))))
@ -74,7 +75,11 @@
(defn throw-on-conflicts! [conflicts]
(throw
(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})))
(defn name-lookup [[_ {:keys [name]}] opts]
@ -118,7 +123,7 @@
:conflicts throw-on-conflicts!})
(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"
([routes]
(linear-router routes {}))
@ -164,11 +169,11 @@
([routes]
(lookup-router routes {}))
([routes opts]
(when-let [route (some impl/contains-wilds? (map first routes))]
(when-let [wilds (seq (filter impl/wild-route? routes))]
(throw
(ex-info
(str "can't create LookupRouter with wildcard routes: " route)
{:route route
(str "can't create LookupRouter with wildcard routes: " wilds)
{:wilds wilds
:routes routes})))
(let [compiled (map #(compile-route % opts) routes)
names (find-names routes opts)
@ -218,7 +223,6 @@
(let [opts (meta-merge default-router-options opts)
routes (resolve-routes data opts)]
(when-let [conflicts (:conflicts opts)]
(when-let [conflicting (conflicting-routes routes)]
(conflicts conflicting)))
((if (some impl/contains-wilds? (map first routes))
linear-router lookup-router) routes opts))))
(when conflicting (conflicts conflicting)))
(router routes opts))))

View file

@ -129,6 +129,9 @@
(defn- catch-all? [segment]
(= \* (first segment)))
(defn wild-route? [[path]]
(contains-wilds? path))
(defn conflicting-routes? [[p1 :as route1] [p2 :as route2]]
(loop [[s1 & ss1] (segments p1)
[s2 & ss2] (segments p2)]