mirror of
https://github.com/metosin/reitit.git
synced 2025-12-29 12:48:25 +00:00
Use exception/fail! in all creation-time exceptions
... to be companioned with a slick error printer
This commit is contained in:
parent
ad92c437e6
commit
950fef88d2
9 changed files with 63 additions and 68 deletions
|
|
@ -1,6 +1,7 @@
|
|||
(ns reitit.core
|
||||
(:require [clojure.string :as str]
|
||||
[reitit.impl :as impl]
|
||||
[reitit.exception :as exception]
|
||||
[reitit.trie :as trie]))
|
||||
|
||||
;;
|
||||
|
|
@ -50,10 +51,7 @@
|
|||
conflicts)))
|
||||
|
||||
(defn throw-on-conflicts! [f conflicts]
|
||||
(throw
|
||||
(ex-info
|
||||
(f conflicts)
|
||||
{:conflicts conflicts})))
|
||||
(exception/fail! (f conflicts) {:conflicts conflicts}))
|
||||
|
||||
;;
|
||||
;; Router
|
||||
|
|
@ -149,11 +147,10 @@
|
|||
(lookup-router compiled-routes {}))
|
||||
([compiled-routes opts]
|
||||
(when-let [wilds (seq (filter impl/wild-route? compiled-routes))]
|
||||
(throw
|
||||
(ex-info
|
||||
(str "can't create :lookup-router with wildcard routes: " wilds)
|
||||
{:wilds wilds
|
||||
:routes compiled-routes})))
|
||||
(exception/fail!
|
||||
(str "can't create :lookup-router with wildcard routes: " wilds)
|
||||
{:wilds wilds
|
||||
:routes compiled-routes}))
|
||||
(let [names (impl/find-names compiled-routes opts)
|
||||
[pl nl] (reduce
|
||||
(fn [[pl nl] [p {:keys [name] :as data} result]]
|
||||
|
|
@ -239,10 +236,9 @@
|
|||
(single-static-path-router compiled-routes {}))
|
||||
([compiled-routes opts]
|
||||
(when (or (not= (count compiled-routes) 1) (some impl/wild-route? compiled-routes))
|
||||
(throw
|
||||
(ex-info
|
||||
(str ":single-static-path-router requires exactly 1 static route: " compiled-routes)
|
||||
{:routes compiled-routes})))
|
||||
(exception/fail!
|
||||
(str ":single-static-path-router requires exactly 1 static route: " compiled-routes)
|
||||
{:routes compiled-routes}))
|
||||
(let [[n :as names] (impl/find-names compiled-routes opts)
|
||||
[[p data result]] compiled-routes
|
||||
p #?(:clj (.intern ^String p) :cljs p)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
(ns reitit.dependency
|
||||
"Dependency resolution for middleware/interceptors.")
|
||||
"Dependency resolution for middleware/interceptors."
|
||||
(:require [reitit.exception :as exception]))
|
||||
|
||||
(defn- providers
|
||||
"Map from provision key to provider. `get-provides` should return the provision keys of a dependent."
|
||||
|
|
@ -8,8 +9,9 @@
|
|||
(into acc
|
||||
(map (fn [provide]
|
||||
(when (contains? acc provide)
|
||||
(throw (ex-info (str "multiple providers for: " provide)
|
||||
{::multiple-providers provide})))
|
||||
(exception/fail!
|
||||
(str "multiple providers for: " provide)
|
||||
{::multiple-providers provide}))
|
||||
[provide dependent]))
|
||||
(get-provides dependent)))
|
||||
{} nodes))
|
||||
|
|
@ -19,8 +21,9 @@
|
|||
[providers k]
|
||||
(if (contains? providers k)
|
||||
(get providers k)
|
||||
(throw (ex-info (str "provider missing for dependency: " k)
|
||||
{::missing-provider k}))))
|
||||
(exception/fail!
|
||||
(str "provider missing for dependency: " k)
|
||||
{::missing-provider k})))
|
||||
|
||||
(defn post-order
|
||||
"Put `nodes` in post-order. Can also be described as a reverse topological sort.
|
||||
|
|
@ -37,8 +40,7 @@
|
|||
(assoc colors node :grey))]
|
||||
[(conj nodes* node)
|
||||
(assoc colors node :black)])
|
||||
:grey (throw (ex-info "circular dependency"
|
||||
{:cycle (drop-while #(not= % node) (conj path node))}))
|
||||
:grey (exception/fail! "circular dependency" {:cycle (drop-while #(not= % node) (conj path node))})
|
||||
:black [() colors]))
|
||||
|
||||
(toposort-seq [nodes path colors]
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
(defn fail!
|
||||
([message]
|
||||
(throw (ex-info message {:type ::exeption})))
|
||||
(throw (ex-info message {::type :exeption})))
|
||||
([message data]
|
||||
(throw (ex-info message (assoc data :type ::exeption)))))
|
||||
(throw (ex-info message (merge {::type ::exeption} data)))))
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@
|
|||
(:require [clojure.string :as str]
|
||||
[clojure.set :as set]
|
||||
[meta-merge.core :as mm]
|
||||
[reitit.trie :as trie])
|
||||
[reitit.trie :as trie]
|
||||
[reitit.exception :as exception])
|
||||
#?(:clj
|
||||
(:import (java.util.regex Pattern)
|
||||
(java.util HashMap Map)
|
||||
|
|
@ -126,10 +127,9 @@
|
|||
(when-not (every? #(contains? path-params %) required)
|
||||
(let [defined (-> path-params keys set)
|
||||
missing (set/difference required defined)]
|
||||
(throw
|
||||
(ex-info
|
||||
(str "missing path-params for route " template " -> " missing)
|
||||
{:path-params path-params, :required required})))))
|
||||
(exception/fail!
|
||||
(str "missing path-params for route " template " -> " missing)
|
||||
{:path-params path-params, :required required}))))
|
||||
|
||||
(defn fast-assoc
|
||||
#?@(:clj [[^clojure.lang.Associative a k v] (.assoc a k v)]
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@
|
|||
(:require [meta-merge.core :refer [meta-merge]]
|
||||
[clojure.pprint :as pprint]
|
||||
[reitit.core :as r]
|
||||
[reitit.impl :as impl]))
|
||||
[reitit.impl :as impl]
|
||||
[reitit.exception :as exception]))
|
||||
|
||||
(defprotocol IntoInterceptor
|
||||
(into-interceptor [this data opts]))
|
||||
|
|
@ -33,10 +34,9 @@
|
|||
#?(:clj clojure.lang.Keyword
|
||||
:cljs cljs.core.Keyword)
|
||||
(into-interceptor [this data {:keys [::registry] :as opts}]
|
||||
(if-let [interceptor (if registry (registry this))]
|
||||
(into-interceptor interceptor data opts)
|
||||
(throw
|
||||
(ex-info
|
||||
(or (if-let [interceptor (if registry (registry this))]
|
||||
(into-interceptor interceptor data opts))
|
||||
(exception/fail!
|
||||
(str
|
||||
"Interceptor " this " not found in registry.\n\n"
|
||||
(if (seq registry)
|
||||
|
|
@ -46,16 +46,15 @@
|
|||
(pprint/print-table [:id :description] (for [[k v] registry] {:id k :description v}))))
|
||||
"see [reitit.interceptor/router] on how to add interceptor to the registry.\n") "\n")
|
||||
{:id this
|
||||
:registry registry}))))
|
||||
:registry registry})))
|
||||
|
||||
#?(:clj clojure.lang.APersistentVector
|
||||
:cljs cljs.core.PersistentVector)
|
||||
(into-interceptor [[f & args :as form] data opts]
|
||||
(when (and (seq args) (not (fn? f)))
|
||||
(throw
|
||||
(ex-info
|
||||
(str "Invalid Interceptor form: " form "")
|
||||
{:form form})))
|
||||
(exception/fail!
|
||||
(str "Invalid Interceptor form: " form "")
|
||||
{:form form}))
|
||||
(into-interceptor (apply f args) data opts))
|
||||
|
||||
#?(:clj clojure.lang.Fn
|
||||
|
|
@ -85,10 +84,9 @@
|
|||
(let [compiled (::compiled opts 0)
|
||||
opts (assoc opts ::compiled (inc ^long compiled))]
|
||||
(when (>= ^long compiled ^long *max-compile-depth*)
|
||||
(throw
|
||||
(ex-info
|
||||
(str "Too deep Interceptor compilation - " compiled)
|
||||
{:this this, :data data, :opts opts})))
|
||||
(exception/fail!
|
||||
(str "Too deep Interceptor compilation - " compiled)
|
||||
{:this this, :data data, :opts opts}))
|
||||
(if-let [interceptor (into-interceptor (compile data opts) data opts)]
|
||||
(map->Interceptor
|
||||
(merge
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@
|
|||
(:require [meta-merge.core :refer [meta-merge]]
|
||||
[clojure.pprint :as pprint]
|
||||
[reitit.core :as r]
|
||||
[reitit.impl :as impl]))
|
||||
[reitit.impl :as impl]
|
||||
[reitit.exception :as exception]))
|
||||
|
||||
(defprotocol IntoMiddleware
|
||||
(into-middleware [this data opts]))
|
||||
|
|
@ -17,10 +18,9 @@
|
|||
#?(:clj clojure.lang.Keyword
|
||||
:cljs cljs.core.Keyword)
|
||||
(into-middleware [this data {:keys [::registry] :as opts}]
|
||||
(if-let [middleware (if registry (registry this))]
|
||||
(into-middleware middleware data opts)
|
||||
(throw
|
||||
(ex-info
|
||||
(or (if-let [middleware (if registry (registry this))]
|
||||
(into-middleware middleware data opts))
|
||||
(exception/fail!
|
||||
(str
|
||||
"Middleware " this " not found in registry.\n\n"
|
||||
(if (seq registry)
|
||||
|
|
@ -30,7 +30,7 @@
|
|||
(pprint/print-table [:id :description] (for [[k v] registry] {:id k :description v}))))
|
||||
"see [reitit.middleware/router] on how to add middleware to the registry.\n") "\n")
|
||||
{:id this
|
||||
:registry registry}))))
|
||||
:registry registry})))
|
||||
|
||||
#?(:clj clojure.lang.APersistentVector
|
||||
:cljs cljs.core.PersistentVector)
|
||||
|
|
@ -61,10 +61,9 @@
|
|||
(let [compiled (::compiled opts 0)
|
||||
opts (assoc opts ::compiled (inc ^long compiled))]
|
||||
(when (>= ^long compiled ^long *max-compile-depth*)
|
||||
(throw
|
||||
(ex-info
|
||||
(str "Too deep Middleware compilation - " compiled)
|
||||
{:this this, :data data, :opts opts})))
|
||||
(exception/fail!
|
||||
(str "Too deep Middleware compilation - " compiled)
|
||||
{:this this, :data data, :opts opts}))
|
||||
(if-let [middeware (into-middleware (compile data opts) data opts)]
|
||||
(map->Middleware
|
||||
(merge
|
||||
|
|
@ -76,11 +75,11 @@
|
|||
|
||||
(defn- ensure-handler! [path data scope]
|
||||
(when-not (:handler data)
|
||||
(throw (ex-info
|
||||
(str "path \"" path "\" doesn't have a :handler defined"
|
||||
(if scope (str " for " scope)))
|
||||
(merge {:path path, :data data}
|
||||
(if scope {:scope scope}))))))
|
||||
(exception/fail!
|
||||
(str "path \"" path "\" doesn't have a :handler defined"
|
||||
(if scope (str " for " scope)))
|
||||
(merge {:path path, :data data}
|
||||
(if scope {:scope scope})))))
|
||||
|
||||
(defn- expand-and-transform
|
||||
[middleware data {:keys [::transform] :or {transform identity} :as opts}]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
(ns reitit.spec
|
||||
(:require [clojure.spec.alpha :as s]
|
||||
[clojure.spec.gen.alpha :as gen]
|
||||
[reitit.core :as reitit]))
|
||||
[reitit.core :as reitit]
|
||||
[reitit.exception :as exception]))
|
||||
|
||||
;;
|
||||
;; routes
|
||||
|
|
@ -119,10 +120,9 @@
|
|||
problems)))
|
||||
|
||||
(defn throw-on-problems! [problems explain]
|
||||
(throw
|
||||
(ex-info
|
||||
(problems-str problems explain)
|
||||
{:problems problems})))
|
||||
(exception/fail!
|
||||
(problems-str problems explain)
|
||||
{:problems problems}))
|
||||
|
||||
(defn validate-route-data [routes spec]
|
||||
(->> (for [[p d _] routes]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
(ns reitit.ring.spec
|
||||
(:require [clojure.spec.alpha :as s]
|
||||
[reitit.middleware :as middleware]
|
||||
[reitit.spec :as rs]))
|
||||
[reitit.spec :as rs]
|
||||
[reitit.exception :as exception]))
|
||||
|
||||
;;
|
||||
;; Specs
|
||||
|
|
@ -19,11 +20,10 @@
|
|||
|
||||
(defn merge-specs [specs]
|
||||
(when-let [non-specs (seq (remove #(or (s/spec? %) (s/get-spec %)) specs))]
|
||||
(throw
|
||||
(ex-info
|
||||
(str "Not all specs satisfy the Spec protocol: " non-specs)
|
||||
{:specs specs
|
||||
:non-specs non-specs})))
|
||||
(exception/fail!
|
||||
(str "Not all specs satisfy the Spec protocol: " non-specs)
|
||||
{:specs specs
|
||||
:non-specs non-specs}))
|
||||
(s/merge-spec-impl (vec specs) (vec specs) nil))
|
||||
|
||||
(defn validate-route-data [routes key spec]
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@
|
|||
(throw
|
||||
(ex-info
|
||||
(str "Can't produce Spec apidocs for " spesification)
|
||||
{:type spesification, :coercion :spec}))))
|
||||
{:spesification spesification, :coercion :spec}))))
|
||||
(-compile-model [_ model name]
|
||||
(into-spec model name))
|
||||
(-open-model [_ spec] spec)
|
||||
|
|
|
|||
Loading…
Reference in a new issue