This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-30
Channels
- # bangalore-clj (1)
- # beginners (9)
- # boot (51)
- # cider (20)
- # cljs-dev (419)
- # clojars (1)
- # clojure (338)
- # clojure-brasil (64)
- # clojure-dev (7)
- # clojure-greece (2)
- # clojure-italy (3)
- # clojure-russia (10)
- # clojure-spec (127)
- # clojure-uk (12)
- # clojurebridge (2)
- # clojurescript (132)
- # core-async (8)
- # cursive (37)
- # datomic (34)
- # dirac (5)
- # events (1)
- # funcool (3)
- # hoplon (39)
- # jobs (3)
- # leiningen (3)
- # off-topic (16)
- # om (44)
- # onyx (7)
- # pedestal (20)
- # protorepl (1)
- # random (1)
- # re-frame (64)
- # reagent (6)
- # specter (4)
- # test-check (9)
- # untangled (17)
- # vim (4)
Hi
I'm migrating from pedestal .4 to .5 and using the new interceptor logic (` defroutes ->
table-routes `)
How do I add an error-dispatch
handler in this new routeing/handler logic?
My actual defroutes looks like this:
(def err-hand
(io.pedestal.interceptor.error/error-dispatch [ctx ex] ,,,)
(defroutes r
[[[^:interceptors [err-hand]
["/srv" {:any srv}]]]])
@souenzzo In the table-routes syntax, interceptors are just an ordinary Clojure vector. So one option would be to prepend err-hand
at the front of each vector.
If you have many routes that gets unwieldy. I often make a helper function that builds the "standard" interceptors that I want every route to have.
Of course, the terse syntax still exists and still inherits interceptors and paths down the tree, so feel free to keep using that syntax if it works better for you.
I found this.
https://github.com/pedestal/pedestal/blob/0.5.1/interceptor/src/io/pedestal/interceptor/error.clj#L24
So (def err-inter {:name ::err :error err-hand})
where (defn err-hand [ctx ex] ,,,)
.
It's not on docs http://pedestal.io/reference/interceptors
I'm still having some problems in migration but I'm progressing
AFAIK, io.pedestal.interceptor.error/error-dispatch
is still the right thing to create.
(def err-hand
(io.pedestal.interceptor.error/error-dispatch [ctx ex] ,,,)
should work fine@eric.vieira will paste
(def error-dispatch
(error-int/error-dispatch [ctx ex]
(assoc ctx :response {:status 400 :body (do
(log/error ex)
(cheshire.core/generate-string (parse-data-exception ex)))})
:else (assoc ctx :response {:status 400 :body (do
(log/error ex)
(cheshire.core/generate-string (parse-data-exception ex)))})
))
(def interceptor-error-handler
{:name ::error-handler
:error error-dispatch
}
)
The body of error-dispatch
should be core.match forms. Here's an example from the test suite:
(def service-error-handler (error-int/error-dispatch [ctx ex] [{:exception-type :java.lang.ArithmeticException :interceptor ::another-bad-one}] (assoc ctx :response {:status 400 :body "Another bad one"}) [{:exception-type :java.lang.ArithmeticException}] (assoc ctx :response {:status 400 :body "A bad one"}) ;; If we don't match, forward it on :else (assoc ctx :io.pedestal.interceptor.chain/error ex)))
(def service-error-handler
(error-int/error-dispatch [ctx ex]
[{:exception-type :java.lang.ArithmeticException
:interceptor ::another-bad-one}] (assoc ctx :response {:status 400 :body "Another bad one"})
[{:exception-type :java.lang.ArithmeticException}] (assoc ctx :response {:status 400 :body "A bad one"})
;; If we don't match, forward it on
:else (assoc ctx :io.pedestal.interceptor.chain/error ex)))
(def service-error-handler
(error-int/error-dispatch [ctx ex]
[{:exception-type :java.lang.ArithmeticException :interceptor ::another-bad-one}]
(assoc ctx :response {:status 400 :body "Another bad one"})
[{:exception-type :java.lang.ArithmeticException}]
(assoc ctx :response {:status 400 :body "A bad one"})
;; If we don't match, forward it on
:else
(assoc ctx :io.pedestal.interceptor.chain/error ex)))