This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-24
Channels
- # beginners (12)
- # cider (3)
- # clara (3)
- # cljs-dev (3)
- # cljsrn (19)
- # clojure (83)
- # clojure-android (1)
- # clojure-dev (15)
- # clojure-dusseldorf (1)
- # clojure-greece (30)
- # clojure-italy (10)
- # clojure-madison (1)
- # clojure-nl (6)
- # clojure-russia (274)
- # clojure-spec (51)
- # clojure-uk (31)
- # clojurescript (38)
- # core-async (7)
- # cursive (11)
- # datascript (1)
- # datomic (63)
- # emacs (10)
- # figwheel (1)
- # hoplon (27)
- # jobs (11)
- # klipse (4)
- # lein-figwheel (1)
- # lumo (6)
- # nyc (1)
- # off-topic (278)
- # om (12)
- # pedestal (10)
- # protorepl (31)
- # re-frame (13)
- # reagent (23)
- # remote-jobs (1)
- # spacemacs (9)
- # untangled (24)
- # yada (54)
is it normal/required in yada (or HTTP) for the response for an endpoint and the error messages for an endpoint to have the same content-type?
Hmm. Well, yada renegotiates the content type against a (currently fixed) set of error content types. So no.
so if I say that my endpoint is eg “text/csv” I can return other types of errors?
Or rather, see the existing set in handler.clj
Needs work from me
To make it more configurable
ah, is that in case of exceptions?
I’m manually checking some things and then returning a {:status 400 :body ...}
Yes. But you can replace the exception interceptor chain with your own
Ah. Then you must also set Content-Type in your reponse. Or better still, use yada's conneg functions to neg one
got that working, thank you!
Good to hear!
@peterwestmacott The way I did it is a bit hacky, but works as well:
(intern 'yada.handler 'error-representations
(ys/representation-seq
(ys/representation-set-coercer
[{:media-type #{"your/content-type"
"application/transit+json"
"application/json;pretty=true;q=0.96"
"text/plain;q=0.9"
"text/html;q=0.8"
"application/edn;q=0.6"
"application/edn;pretty=true;q=0.5"}
:charset charset/platform-charsets}])))
Then override/define defmethod render-error “your/content-type”.Has anyone explored the idea of avoiding side-effect-y/mutate-y code in the method handler?
The phonebook example, for example, does (db/add-entry db ...)
somewhere in the :post handler.
@jonathanj where would you do it?
I haven’t really thought about this super hard, so don’t presume I have anything resembling a good idea. 😉
If the handler returns data that describes what it expects to happen, something else could take that and actually do the work.
@jonathanj you might be interested in looking at event sourcing as a pattern 🙂
If there was a function that was handed the request and returned some kind of query/selector data that the handler was called with (eventually) that sort of approaches a solution.
@jonathanj I’m not entirely sure of your problem, but isn’t Manifold deferred something you could utilize here?
Testing something like this and avoiding leaking all manner of business logic into HTTP handlers (that you may forget to do somewhere else) seems a lot easier.
Okay, well when you implement your component you can implement an IQuery interface that is called (by om.next’s internal machinery) and should return a piece of data that describes your query (with some kind of datalog-ish looking syntax), internally it gives the query to something (you specified elsewhere), and eventually the data is passed to your component’s render function.
It was just a thought that popped up in my head, in terms of: HTTP request -> handler declares what data it would like to receive -> something realises that query -> your HTTP handler is called with that data -> return some data that describes what the handler intends to happen -> something realises that
HTTP handler = resource in Yada’s terms. Resource declares what data it would like to receive. This depends on the request. It can be realized anywhere using an interceptor?
An interceptor is just a function from context to context. You can insert them anywhere into an existing chain.
E.g. I made this interceptor to manipulate the Schema to allow an extra parameter:
(defn allow-response-filter-in-schema
[ctx]
(if (rb/body-schema ctx)
(update-in ctx [:resource :methods
(:method ctx) :parameters :body]
(fn [schema]
(if (map? schema)
(assoc schema
(s/optional-key :response/filter)
s/Any)
schema)))
ctx))
And I inserted it like this:
(insert-interceptor
i/process-request-body
allow-response-filter-in-schema)