yada

2018-03-01T16:03:29.000891Z

is there a way to set global options?

2018-03-01T16:03:49.000663Z

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

dominicm 2018-03-01T16:12:34.000151Z

@wdullaer we do that by postwalking the bidi structure.

2018-03-01T17:45:06.000708Z

do you have an example of what that looks like?

2018-03-01T17:45:41.000370Z

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

malcolmsparks 2018-03-01T18:16:16.000351Z

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

malcolmsparks 2018-03-01T18:17:15.000607Z

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

malcolmsparks 2018-03-01T18:17:34.000461Z

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

malcolmsparks 2018-03-01T18:18:36.000535Z

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

malcolmsparks 2018-03-01T18:18:50.000032Z

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

danielcompton 2018-03-01T20:35:55.000460Z

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