Skip to content

Commit 29bcc29

Browse files
committed
Add with-logging helper to add logging to exception handlers
1 parent 9d916d5 commit 29bcc29

3 files changed

Lines changed: 46 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Unreleased
2+
3+
- Added `compojure.api.exception/with-logging` helper to add logging to exception handlers.
4+
- Check extended wiki guide on [exception handling](https://github.com/metosin/compojure-api/wiki/Exception-handling#logging)
5+
16
## 0.24.4 (13.1.2016)
27

38
**[compare](https://github.com/metosin/compojure-api/compare/0.24.3...0.24.4)**

src/compojure/api/exception.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@
5757
:else "parse-exception")
5858
:message (.getMessage cause)})))
5959

60+
;;
61+
;; Logging
62+
;;
63+
64+
(defn with-logging [handler]
65+
(fn [^Exception e data req]
66+
(logging/log! :error e (.getMessage e))
67+
(handler e data req)))
68+
6069
;;
6170
;; Mappings from other Exception types to our base types
6271
;;

test/compojure/api/middleware_test.clj

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns compojure.api.middleware-test
22
(:require [compojure.api.middleware :refer :all]
3+
[compojure.api.exception :as ex]
34
[midje.sweet :refer :all]
45
[ring.util.http-response :refer [ok]]
56
[ring.util.http-status :as status]
@@ -39,23 +40,43 @@
3940

4041
(ring.util.test/string-input-stream "foobar") false false))
4142

43+
(def default-options (:exceptions api-middleware-defaults))
44+
4245
(facts "wrap-exceptions"
4346
(with-out-str
44-
(without-err
45-
(let [exception (RuntimeException. "kosh")
46-
exception-class (.getName (.getClass exception))
47-
handler (-> (fn [_] (throw exception))
48-
(wrap-exceptions {}))]
47+
(without-err
48+
(let [exception (RuntimeException. "kosh")
49+
exception-class (.getName (.getClass exception))
50+
handler (-> (fn [_] (throw exception))
51+
(wrap-exceptions default-options))]
4952

50-
(fact "converts exceptions into safe internal server errors"
51-
(handler {}) => (contains {:status status/internal-server-error
52-
:body (contains {:class exception-class
53-
:type "unknown-exception"})})))))
53+
(fact "converts exceptions into safe internal server errors"
54+
(handler {}) => (contains {:status status/internal-server-error
55+
:body (contains {:class exception-class
56+
:type "unknown-exception"})})))))
5457

5558
(with-out-str
5659
(without-err
5760
(fact "Slingshot exception map type can be matched"
5861
(let [handler (-> (fn [_] (throw+ {:type ::test} (RuntimeException. "kosh")))
59-
(wrap-exceptions {:handlers {::test (fn [ex _ _] {:status 500 :body "hello"})}}))]
62+
(wrap-exceptions (assoc-in default-options [:handlers ::test] (fn [ex _ _] {:status 500 :body "hello"}))))]
6063
(handler {}) => (contains {:status status/internal-server-error
61-
:body "hello"}))))))
64+
:body "hello"})))))
65+
66+
(without-err
67+
(fact "Default handler logs exceptions to console"
68+
(let [handler (-> (fn [_] (throw (RuntimeException. "kosh")))
69+
(wrap-exceptions default-options))]
70+
(with-out-str (handler {})) => "ERROR kosh\n")))
71+
72+
(without-err
73+
(fact "Default request-parsing handler does not log messages"
74+
(let [handler (-> (fn [_] (throw (ex-info "Error parsing request" {:type ::ex/request-parsing} (RuntimeException. "Kosh"))))
75+
(wrap-exceptions default-options))]
76+
(with-out-str (handler {})) => "")))
77+
78+
(without-err
79+
(fact "Logging can be added to a exception handler"
80+
(let [handler (-> (fn [_] (throw (ex-info "Error parsing request" {:type ::ex/request-parsing} (RuntimeException. "Kosh"))))
81+
(wrap-exceptions (assoc-in default-options [:handlers ::ex/request-parsing] (ex/with-logging ex/request-parsing-handler))))]
82+
(with-out-str (handler {})) => "ERROR Error parsing request\n"))))

0 commit comments

Comments
 (0)