mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 00:41:12 +00:00
Docs
This commit is contained in:
parent
fda6a1e06f
commit
68eb09b68b
1 changed files with 72 additions and 9 deletions
|
|
@ -112,22 +112,85 @@ Middleware is applied correctly:
|
|||
; {:status 200, :body [:api :admin :db :delete :handler]}
|
||||
```
|
||||
|
||||
# Not found
|
||||
# Default handler
|
||||
|
||||
If no routes match, `nil` is returned, which is not understood by Ring.
|
||||
By default, if no routes match, `nil` is returned, which is not valid response in Ring:
|
||||
|
||||
Enabling custom error messages:
|
||||
```clj
|
||||
(defn handler [_]
|
||||
{:status 200, :body ""})
|
||||
|
||||
(def app
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
["/ping" handler])))
|
||||
|
||||
(app {:uri "/invalid"})
|
||||
; nil
|
||||
```
|
||||
|
||||
Setting the default-handler as a second argument to `ring-handler`:
|
||||
|
||||
```clj
|
||||
(def app
|
||||
(some-fn
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
["/ping" handler]))
|
||||
(constantly {:status 404})))
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
["/ping" handler])
|
||||
(constantly {:status 404, :body ""})))
|
||||
|
||||
(app {:uri "/invalid"})
|
||||
; {:status 404}
|
||||
; {:status 404, :body ""}
|
||||
```
|
||||
|
||||
To get more correct http error responses, `ring/create-default-handler` can be used. It differentiates `:not-found` (no route matched), `:method-not-accepted` (no method matched) and `:not-acceptable` (handler returned `nil`).
|
||||
|
||||
With defaults:
|
||||
|
||||
```clj
|
||||
(def app
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
[["/ping" {:get handler}]
|
||||
["/pong" (constantly nil)]])
|
||||
(ring/create-default-handler)))
|
||||
|
||||
(app {:request-method :get, :uri "/ping"})
|
||||
; {:status 200, :body ""}
|
||||
|
||||
(app {:request-method :get, :uri "/"})
|
||||
; {:status 404, :body ""}
|
||||
|
||||
(app {:request-method :post, :uri "/ping"})
|
||||
; {:status 405, :body ""}
|
||||
|
||||
(app {:request-method :get, :uri "/pong"})
|
||||
; {:status 406, :body ""}
|
||||
```
|
||||
|
||||
With custom responses:
|
||||
|
||||
```clj
|
||||
(def app
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
[["/ping" {:get handler}]
|
||||
["/pong" (constantly nil)]])
|
||||
(ring/create-default-handler
|
||||
{:not-found (constantly {:status 404, :body "kosh"})
|
||||
:method-not-allowed (constantly {:status 405, :body "kosh"})
|
||||
:not-acceptable (constantly {:status 406, :body "kosh"})})))
|
||||
|
||||
(app {:request-method :get, :uri "/ping"})
|
||||
; {:status 200, :body ""}
|
||||
|
||||
(app {:request-method :get, :uri "/"})
|
||||
; {:status 404, :body "kosh"}
|
||||
|
||||
(app {:request-method :post, :uri "/ping"})
|
||||
; {:status 405, :body "kosh"}
|
||||
|
||||
(app {:request-method :get, :uri "/pong"})
|
||||
; {:status 406, :body "kosh"}
|
||||
```
|
||||
|
||||
# Async Ring
|
||||
|
|
|
|||
Loading…
Reference in a new issue