Fork me on GitHub
#yada
<
2018-03-01
>
wdullaer16:03:29

is there a way to set global options?

wdullaer16:03:49

for example, I want to ensure that all errors are serialized as json, rather than edn

dominicm16:03:34

@wdullaer we do that by postwalking the bidi structure.

wdullaer17:03:06

do you have an example of what that looks like?

wdullaer17:03:41

all my resources specify produces: application/json, but for example schema errors will still be returned as edn

malcolmsparks18:03:16

yes, yada does that on purpose -we've had a lot of problems with cheshire serializing exceptions to json - you tend to end up with cheshire serialization issues

malcolmsparks18:03:15

if you look that the yada code-base, the default error interceptor chain starts by renegotiating the content-type of the response - there are some content-types that the server can produce, and you'd need to extend that to override them

malcolmsparks18:03:34

it's possible if you override with your own error interceptor chain

malcolmsparks18:03:36

if you look in src/yada/walk.clj there's an example of using clojure.walk to walk the bidi tree looking for yada resources and updating them

malcolmsparks18:03:50

really, it's just a data structure which you can manipulate just like any other clojure data structure

danielcompton20:03:55

@wdullaer I have code like this for walking:

(defn append-error-interceptor [res point & interceptors]
  (update res :error-interceptor-chain
          (partial mapcat (fn [i]
                            (if (= i point)
                              (concat [i] interceptors)
                              [i])))))

(defn private-cache-control-headers [ctx]
  (assoc-in ctx [:response :headers "Cache-Control"] "no-cache"))

(defn public-cache-control-headers [ctx]
  (assoc-in ctx [:response :headers "Cache-Control"] "max-age=300"))

(defn static-content [env]
  ["/static/"
   [["" (-> (cp-resource/new-classpath-resource "public")
            (assoc :id :deps.resources/static)
            (yada/handler)
            (yada.handler/append-interceptor sec/security-headers
              (if (= :prod env)
                public-cache-control-headers
                identity)))]]])

(defn update-resources [routes f & args]
  (walk/postwalk
    (fn [x]
      (if (instance? Resource x)
        (resource (apply f x args))
        x))
    routes))


;; ... inside my routes table

(-> ... ["" [(index/index-routes postgres config)
                 
                    (account/account-routes postgres email-service stripe)
                    (login/auth-routes postgres session-store email-service)
                   ]]
(update-resources yada.handler/prepend-interceptor
                 (interceptors/begin-mock-trace))
               (update-resources yada.handler/append-interceptor i/logging
                 (interceptors/end-mock-trace (traces/-get-tracer traces)))
               (update-resources yada.handler/append-interceptor i/select-representation
                 csrf/check-not-forged)
               (update-resources append-error-interceptor i/logging
                 (error-reporters/make-report-error-interceptor reporter))