is there a way to set global options?
for example, I want to ensure that all errors are serialized as json, rather than edn
@wdullaer we do that by postwalking the bidi structure.
do you have an example of what that looks like?
all my resources specify produces: application/json, but for example schema errors will still be returned as edn
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
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
it's possible if you override with your own error interceptor chain
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
really, it's just a data structure which you can manipulate just like any other clojure data structure
@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))