Support custom stacktrace middleware

Adds new option :stacktrace-middleware, a middleware function that
handles exceptions.

See #20
This commit is contained in:
Magnar Sveen 2014-09-29 15:49:49 +02:00
parent b49a910fcb
commit cffbe02375
3 changed files with 29 additions and 15 deletions

View file

@ -17,4 +17,5 @@
(:ring project) (:ring project)
(-> project :ring :adapter) (-> project :ring :adapter)
{:init (load-var (-> project :ring :init)) {:init (load-var (-> project :ring :init))
:destroy (load-var (-> project :ring :destroy))}))) :destroy (load-var (-> project :ring :destroy))
:stacktrace-middleware (load-var (-> project :ring :stacktrace-middleware))})))

View file

@ -52,7 +52,8 @@
(defn- add-stacktraces [handler options] (defn- add-stacktraces [handler options]
(if (stacktraces? options) (if (stacktraces? options)
(wrap-stacktrace handler) ((or (:stacktrace-middleware options)
wrap-stacktrace) handler)
handler)) handler))
(defn- add-auto-reload [handler options] (defn- add-auto-reload [handler options]
@ -73,16 +74,17 @@
(defn serve (defn serve
"Start a web server to run a handler. Takes the following options: "Start a web server to run a handler. Takes the following options:
:port - the port to run the server on :port - the port to run the server on
:join? - if true, wait for the server to stop :join? - if true, wait for the server to stop
:init - a function to run before the server starts :init - a function to run before the server starts
:destroy - a function to run after the server stops :destroy - a function to run after the server stops
:open-browser? - if true, open a web browser after the server starts :open-browser? - if true, open a web browser after the server starts
:browser-uri - the path to browse to when opening a browser :browser-uri - the path to browse to when opening a browser
:stacktraces? - if true, display stacktraces when an exception is thrown :stacktraces? - if true, display stacktraces when an exception is thrown
:auto-reload? - if true, automatically reload source files :stacktrace-middleware - a middleware that handles stacktraces
:reload-paths - seq of src-paths to reload on change - defaults to [\"src\"] :auto-reload? - if true, automatically reload source files
:auto-refresh? - if true, automatically refresh browser when source changes :reload-paths - seq of src-paths to reload on change - defaults to [\"src\"]
:auto-refresh? - if true, automatically refresh browser when source changes
If join? is false, a Server object is returned." If join? is false, a Server object is returned."
{:arglists '([handler] [handler options])} {:arglists '([handler] [handler options])}

View file

@ -48,4 +48,15 @@
(with-server (test-server {:handler exception-handler}) (with-server (test-server {:handler exception-handler})
(let [body (:body (http-get 3000 ""))] (let [body (:body (http-get 3000 ""))]
(is (re-find #"java\.lang\.Exception" body)) (is (re-find #"java\.lang\.Exception" body))
(is (re-find #"testing" body)))))) (is (re-find #"testing" body)))))
(testing "custom stacktrace middleware"
(let [middleware (fn [handler]
(fn [req]
(try (handler req)
(catch Exception e
{:body "Hello"}))))]
(with-server (test-server {:handler exception-handler
:stacktrace-middleware middleware})
(is (= (:body (http-get 3000 ""))
"Hello"))))))