Skip to content

Commit

Permalink
feat(exceptions): optional duct/logger
Browse files Browse the repository at this point in the history
if no logger provided in options, just use pretty print for logging
  • Loading branch information
kkharji committed Jan 2, 2022
1 parent 5779f42 commit f966f83
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Full configuration demo:

:duct.reitit/options
{:muuntaja true ; default true, can be a modified instance of muuntaja.
:logger #ig/ref :duct/logger ;; logger to be used.
:coercion ;; coercion configuration, default nil.
{:coercer 'spec ; coercer to be used
:pretty-print? true ; whether to pretty print coercion errors requires expound
Expand Down Expand Up @@ -86,14 +87,15 @@ Extra reitit and ring options
- `:error-formater` custom function to format the return body.
- `:environment`: environment variables to be injected to handlers.
- `:middlewares`: global middleware to be passed to reitit middleware key with the default once.
- `:logger`: logger to be used in logging. TODO: make it default to duct/logger if available
- `:cross-origin` cross-origin resource sharing configuration, In development, the origin
will always be a wildcard as the example above. valid keys: `:headers, :origin, :methods`
- `:exceptions`
- `:handlers`: basic wrapper around [ring-reitit-exception-middleware]. It
expects a map of exception classes or
`reitit.ring.middleware.exception` keys like wrap or default, and a
function that takes `[exception request]`.
- `:log?` whether to log exceptions using `duct.logger`, default true if `duct.logger` exists.
- `:log?` whether to log exceptions using `duct/logger`, default true if `duct/logger` exists.
- `:pretty?` whether to make log exceptions easier to read.

[ring-reitit-exception-middleware]: https://cljdoc.org/d/metosin/reitit/0.5.15/doc/ring/exception-handling-with-ring#exceptioncreate-exception-middleware
Expand Down
6 changes: 4 additions & 2 deletions src/duct/reitit/middleware/exception.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
(with-default-exceptions
(:handlers exception)
coercion-handlers
(when (and (:log? exception) logger)
(when (:log? exception)
{::exception/wrap
(fn [handler e request]
(logger/log logger :error (format-exception-log e request (:pretty? exception)))
(if logger
(logger/log logger :error (format-exception-log e request (:pretty? exception)))
(pprint/pprint (format-exception-log e request false)))
(handler e request))}))))


2 changes: 1 addition & 1 deletion src/duct/reitit/module.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
(defmethod init-key :duct.module/reitit [_ _]
(fn [{:duct.reitit/keys [registry routes options] :as config}]
(let [config (merge default-config config)
options (assoc (merge default-options options) :logger (ig/ref :duct/logger))
options (merge default-options options)
namespaces (get-namespaces config)
registry (resolve-registry namespaces registry)
config (dissoc (with-registry config registry) :duct.reitit/options :duct.reitit/routes)
Expand Down
4 changes: 2 additions & 2 deletions test/duct/reitit/module_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
:foo/database [{:author "tami5"}]
:foo/index-path "resources/index.html"
:foo.handler/exceptions {}
:duct.logger/timbre {:set-root-config? true
:level :trace}
:duct.logger/timbre {:set-root-config? true :level :trace}

:duct.reitit/routes
[["/" :index]
Expand All @@ -50,6 +49,7 @@
:exception {:handlers (ig/ref :foo.handler/exceptions)
:log? true ;; default true.
:pretty? true} ;; default in dev
:logger (ig/ref :duct/logger)
:coercion ;; coercion configuration, default nil.
{:coercer 'spec ; coercer to be used
:pretty-coercion? true ; whether to pretty print coercion errors
Expand Down

0 comments on commit f966f83

Please sign in to comment.