reitit/doc/basics/route_conflicts.md

111 lines
2.3 KiB
Markdown
Raw Normal View History

2017-12-10 14:57:09 +00:00
# Route Conflicts
2017-09-18 05:30:03 +00:00
2019-04-02 19:44:24 +00:00
We should fail fast if a router contains conflicting paths or route names.
2017-09-18 05:30:03 +00:00
When a `Router` is created via `reitit.core/router`, both path and route name conflicts are checked automatically. By default, in case of conflict, an `ex-info` is thrown with a descriptive message. In some (legacy api) cases, path conflicts should be allowed and one can override the path conflict resolution via `:conflicts` router option or via `:conflicting` route data.
2017-09-18 05:30:03 +00:00
2018-07-21 05:49:20 +00:00
## Path Conflicts
Routes with path conflicts:
2017-09-18 05:30:03 +00:00
```clj
(require '[reitit.core :as r])
2017-09-18 05:30:03 +00:00
(def routes
[["/ping"]
["/:user-id/orders"]
["/bulk/:bulk-id"]
["/public/*path"]
["/:version/status"]])
```
2018-07-21 05:49:20 +00:00
Creating router with defaults:
2017-09-18 05:30:03 +00:00
```clj
(r/router routes)
2018-07-21 05:49:20 +00:00
; CompilerException clojure.lang.ExceptionInfo: Router contains conflicting route paths:
2017-09-18 05:30:03 +00:00
;
; -> /:user-id/orders
2017-09-18 05:30:03 +00:00
; -> /public/*path
; -> /bulk/:bulk-id
;
; -> /bulk/:bulk-id
2017-09-18 05:30:03 +00:00
; -> /:version/status
;
; -> /public/*path
2017-09-18 05:30:03 +00:00
; -> /:version/status
;
```
2018-07-21 05:49:20 +00:00
To ignore the conflicts:
```clj
(r/router
routes
{:conflicts nil})
; => #object[reitit.core$quarantine_router$reify
2018-07-21 05:49:20 +00:00
```
To just log the conflicts:
2017-09-18 05:30:03 +00:00
```clj
(require '[reitit.exception :as exception])
(r/router
2017-09-18 05:30:03 +00:00
routes
2018-07-21 05:49:20 +00:00
{:conflicts (fn [conflicts]
(println (exception/format-exception :path-conflicts nil conflicts)))})
2018-07-21 05:49:20 +00:00
; Router contains conflicting route paths:
2017-09-18 05:30:03 +00:00
;
; -> /:user-id/orders
2017-09-18 05:30:03 +00:00
; -> /public/*path
; -> /bulk/:bulk-id
;
; -> /bulk/:bulk-id
2017-09-18 05:30:03 +00:00
; -> /:version/status
;
; -> /public/*path
2017-09-18 05:30:03 +00:00
; -> /:version/status
;
; => #object[reitit.core$quarantine_router$reify]
```
Alternatively, you can ignore conflicting paths individually via `:conflicting` in route data:
```clj
(def routes
[["/ping"]
["/:user-id/orders" {:conflicting true}]
["/bulk/:bulk-id" {:conflicting true}]
["/public/*path" {:conflicting true}]
["/:version/status" {:conflicting true}]])
; => #'user/routes
(r/router routes)
; => #object[reitit.core$quarantine_router$reify]
2017-09-18 05:30:03 +00:00
```
2018-07-21 05:49:20 +00:00
## Name conflicts
Routes with name conflicts:
```clj
(def routes
[["/ping" ::ping]
["/admin" ::admin]
["/admin/ping" ::ping]])
```
Creating router with defaults:
```clj
(r/router routes)
;CompilerException clojure.lang.ExceptionInfo: Router contains conflicting route names:
;
;:reitit.core/ping
;-> /ping
;-> /admin/ping
;
```
There is no way to disable the name conflict resolution.