Fork me on GitHub
#pedestal
<
2016-09-30
>
souenzzo18:09:53

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}]]]])

mtnygard19:09:23

@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.

mtnygard19:09:50

E.g.,

#{["/srv" :any [err-hand srv]]}

mtnygard19:09:27

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.

mtnygard19:09:55

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.

souenzzo19:09:55

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

mtnygard19:09:33

AFAIK, io.pedestal.interceptor.error/error-dispatch is still the right thing to create.

mtnygard19:09:20

I don't think you need to further wrap it with err-inter like that.

mtnygard19:09:36

(def err-hand
  (io.pedestal.interceptor.error/error-dispatch [ctx ex] ,,,)
should work fine

mtnygard19:09:12

Can you paste more of the code?

eric.vieira20:09:56

(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
   }
  )

mtnygard20:09:15

Something definitely doesn't look right.

mtnygard20:09:49

The body of error-dispatch should be core.match forms. Here's an example from the test suite:

mtnygard20:09:55

(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)))

mtnygard20:09:01

(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)))

mtnygard20:09:14

One sec, let me reformat for clarity

mtnygard20:09:49

(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)))

mtnygard20:09:19

The body is in pairs of forms. The first form is a pattern match. The second clause is what to execute if the pattern matches.

mtnygard20:09:38

And the return value from error-dispatch is an interceptor, so no need to further wrap it in another interceptor.