This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-08-17
Channels
- # admin-announcements (1)
- # aleph (1)
- # architecture (1)
- # bangalore-clj (14)
- # beginners (15)
- # boot (89)
- # braveandtrue (1)
- # cider (1)
- # cljs-dev (33)
- # cljsjs (1)
- # cljsrn (147)
- # clojure (149)
- # clojure-quebec (1)
- # clojure-russia (82)
- # clojure-spec (18)
- # clojure-taiwan (2)
- # clojure-uk (15)
- # clojurescript (97)
- # cursive (11)
- # datomic (22)
- # funcool (2)
- # hoplon (53)
- # immutant (16)
- # jobs-rus (8)
- # lambdaisland (1)
- # off-topic (13)
- # om (7)
- # onyx (58)
- # parinfer (6)
- # planck (19)
- # protorepl (2)
- # re-frame (17)
- # reagent (201)
- # rum (6)
- # specter (9)
- # test-check (68)
- # untangled (47)
- # yada (94)
when I want to return another http status (and body) from the response-function, is throwing an exception the best way to do that? Now, I have something like:
(defn response ""
[ctx]
(let [pars (:parameters ctx)]
(if (spec/valid ::pars-spec pars)
{...}
(throw (ex-info "Invalid parameters" {:status 400 :error ...})))))
... it should be spec/valid?
(with a question mark)
It's a way to do it. You can also:
(assoc ctx :response
{:status 200
:body "content"})
This makes more sense. I don't like throwing exceptions 'everywhere'.
@dominicm: that will nullify any response headers set. Better to (assoc (:response ctx) :status 200 :body "content")
allright, thx!
... doesn't seem to work as expected. When I change
(throw (ex-info "Invalid parameters" {:status 400 :error ...}))
with
(assoc (:response ctx)
:status 400
:error ...
:body "Invalid profile data")
I get response code 0
(response body: no content).
If I add ctx
as return value, I get response code 204
(response body: no content).what method are you using, I'll check the code
If it isn't working I'd like to understand why
@kurt-yagram: can you share a bit more code that will reveal the method name you're using? perhaps the whole resource model would help
you definitely should not be getting status of 0
yeah, just a sec... I'll post the code and remove irrelevant parts
np - take your time. I'm sure it's something I can help with - if there's a bug in yada I can fix that too -although lots of people use the explicit response feature so I'm fairly surprised it isn't working for you. I'll do my best to help
@kurt-yagram: is it really 4.25am for you there?
i like your profile picture - is that a recent photo? if so, might explain why you're up at 4.25 🙂
euh, no, it's 1.26 pm - lol, but yes, photo is recent. Does it show 4.26 am? slack tells me it's 1.26 pm
that code looks fine to me, i'm going to try to replicate here
yeah, but slack is very fallible 🙂
well, I'm using swagger ui, so I don't know if it could be a swagger/swagger-ui thing or not. It works with throwing exceptions...
Ah, that might explain it. Try using curl or postman too, so you can distinguish between whether it's a yada thing or swagger-ui thing - I've seen swagger ui report status as 0 when it isn't
swagger-ui is a nice, but complex beast
ah, ok... will try 🙂
that looks different... I do get the response body... How do I get the http status from curl?
got it: add -w %{http_code}
seems to be a swagger ui thing.
curl -i
it will show the status and all the response headers
that's what I was looking for, thx !
so, curl works fine... it's swagger-ui that has issues with the response
curl -v will show the request stuff too, fyi
what's the response code - is it a 400?
yes, it is.
so yes, it works fine...
is the response body and content-type aligned?
maybe swagger-ui is trying to parse the response body and failing - mind you, this is something yada tries to get right for you
well, I first should make the body a little more like a map 🙂
now it's just a string
allright...
your PUT code is working for me, but...
`(assoc (:response ctx) {:status 400 :body "Invalid profile data"}`
yeah, that's wrong
that's wrong - you either need 'merge' there, or remove the curlies
fixed that part 🙂
removed the curlies
ah ok, 🙂
ok, and now, if I make the body a map instead of a string, it works in swagger-ui as well
@dominicm: not sure that 747 issue is relevant here but I could be wrong
how easy life can be
@malcolmsparks: If swagger is trying to parse a JSON object and failing, could that cause it? Because "Invalid profile data" is invalid JSON
Yes, that's it
that's right
That's why swagger-ui couldn't handle the response - which makes perfect sense. My application didn't do what it promised 😛.
typically what you do when you've got multiple possible content-types, is you ask yada which content-type was negotiated - with (yada/content-type ctx)
then you switch on the result - say if it's text/html you can proceed to return a string, but if it's application/json you need to return a map (any value you do return will be coerced into JSON)
That reminds me I need to do my template-record hybrid thing, which becomes JSON on demand
yada does all the content-negotiation for you, so by the time you get to decide the body, you already know what content-type to produce. That's one of the really big things yada offers over pedestal and others
liberator does conneg, but it's a bit more primitive and doesn't do all the content axes
most of my API services emit application/json and application/edn by default
well, now I'm busy here: if I want to use declared responses: do I understand it well that if I return just an integer (e.g. 400) and in my yada/resource
map I add a :responses
key, I can put the assoc
-part inside that function? - Well, I just try 😛
See the docs, chapter 10
yeah, I was looking at them.
hello I checked out the local tutorial, but with all that tooling I'm overwhelmed by how can I setup yada with my simple resource project, no need for reload or anything, just a simple webserver...
I tried with a ring middleware but couldnt make it work
I think it's something about the run-jetty function that doesnt seem to work with yada
@vinnyataide: sorry you rang into trouble. did you look at https://juxt.pro/yada/manual/index.html#the-simple-way-construct-your-own ?
aw sorry man, I couldnt figure how to pass after point 3 then I came here
maybe a little bit of reindexing the manual could help the newcomers, but I know that I shouldve keep reading, thanks, gonna try it now
you can't run yada with run-jetty because it's asynchronous - that's why you have to use aleph which supports it
it's possible to write an adaptor which could deref the response, but doing so with defeat the point of yada being asynchronous in the first place
@malcolmsparks: that's more than clear, I'm now on the track
yeah, I didn't know anything about java servers, since I'm a node dev
thanks for the feedback on the docs, I'm working hard on improving them and making them more complete - but yes, some reindexing would help
in a sense, as a node dev you'll be more familiar with the yada mode of operation than Ring, because in yada everything is async
cool, that's what made me choose, now I know a little more about the underparts
there's a bit of a debate in the clojure/jvm world about whether async is overkill - of course in nodejs there's nothing but async because you can't have multiple threads
it's strange since ring is the defacto server solution for clojure
I wrote a blog article a couple of months back that touches on these points: https://juxt.pro/blog/posts/yada-2.html
but yes, Ring is definitely the de-facto approach
I don't think that it's overkill... If you need more control you can use threads, but async operations in the request response need to be as fast as possible
hence the node popularity
all right
gonna read it later
congratulations on the lib, it's a really breath for async programmers to work
thanks - i'll continue to invest in improving the docs, especially for beginners.
giving my 2cent, today's everyday little business solution needs much more speed in the I/O than in proccess handling... IDK about other peoples vision since I'm solely invested in web development
the server is working perfectly
great to hear