Fork me on GitHub
#yada
<
2016-06-01
>
richiardiandrea00:06:21

so I got the point where I think I am not using yada in the right way...I am basically wrapping my computations so that it does not throw because I want to have control over the returned payload in case of exception..but...I feel I can ask yada to customize this...for instance, I do:

(instance? PSQLException res) {:status 400
                                   :message (str/trim (first (or (re-find #"Detail:(.*)" (.getMessage res))
                                                                 (re-find #"ERROR:(.*)" (.getMessage res)))))
                                   :error {:code -32603}}
in a normalize function inside the :response callback...maybe there is a way to just say to yada: "hey, when there is an exception, can you please pass by this normalize function?

richiardiandrea00:06:38

this way I don't need to use try...`catch`

richiardiandrea00:06:18

(btw up there is JSON-RPC 2.0)

malcolmsparks06:06:47

@richiardiandrea You are using an 'escape hatch' which is ok. You are determining the status code is 400 is there is a PSQLException. That is not something that yada can figure out by itself. You should catch Exceptions that you can handle. If you let them through yada will see uncaught exceptions as 500. If you are sure 400 is the appropriate code in your case, you're doing this right. I would normally question whether all PSQLExceptions are the client's fault, but it's your app.

malcolmsparks06:06:06

@tangrammer: logging the response body is possible if you make it a string or byte-array. But if it's a stream or nio channel you can't.

tangrammer07:06:56

@malcolmsparks: at last i used this approach for logging middleware

(defn log-handler  [ctx]
  (if  (>= (-> ctx :response :status) 400)
    (let [body (bs/to-string (-> ctx :response :body ))]
      (log/info "CALL >>  "  (:response ctx) " :: " (-> ctx :request :uri) " :::: " (-> ctx :parameters) " :: " body)
      (assoc-in ctx [:response :body] (bs/convert body java.nio.ByteBuffer)))
    (log/info "CALL >>  "  (:response ctx) " :: " (-> ctx :request :uri) " :::: " (-> ctx :parameters))))


resource=> {...
 :logger log-handler
...}

so, only if i got >= 400 status code then i convert the body to string but i create a new buffer based on this string and attach it to the ctx

tangrammer07:06:36

馃檪 it's a naive approach due that it doesn't consider special body types but is a starting point

malcolmsparks08:06:01

You have to be careful logging large bodies. If you have a slow consumer, parts of the body could be sent at different times. When exactly should you log? At the start? At the end? I think the only compromise is to log metadata but not the body itself, unless it's a string. Coercing a body to a string could be very expensive. Try doing that with a video file boxset of Game of Thrones....

imre08:06:21

Now that clojure.spec is announced, are there plans to support it in yada?

malcolmsparks08:06:40

Yes. It's a great excuse for yada 2.0 ;)

malcolmsparks08:06:38

But I'm holding off until Clojure 1.9 is out because yada is a heavy user of schema and spec needs the coercers (conformers)

imre08:06:32

have you heard anything about the possibility of coercers for spec?

imre08:06:49

that's one of the reasons I'm holding off as well

malcolmsparks08:06:20

I think they are conformers, but yes it's early days - I don't want to be an early adopter!

imre08:06:28

馃檪 well, I'm an early adopter I think

dominicm08:06:13

RE conformers, Alex M(iller has been cautioning against them. They are fairly simple to add onto spec though, there is a gist demo'ing a working implementation.

mccraigmccraig08:06:43

@dominicm: what was his reasoning against conformers ?

dominicm08:06:19

@mccraigmccraig: Perhaps I am misremembering, I can't find the comment now. But I did dig up the code in my process https://gist.github.com/gtrak/9b6d16d0423b284292dbbdd095bf91e9

dominicm08:06:23

clojure.spec can do runtime validation, but in my opinion, it's not well catered for.

imre09:06:34

hmm doesn't that defeat its purpose?

dominicm09:06:11

@imre The case for spec is strongly towards function specifications during development only

dominicm09:06:56

There is a comment by Rich that you can use valid? to do it explicitly, but in my opinion, it's design doesn't suit that particularly well.

imre09:06:22

hmm... I can see that as a serious blocker of adoption of spec

imre09:06:25

runtime validation and detailed validation failure reporting are needed in a lot of business cases, if I cannot use spec properly for that, why should I double the dev and maintenance effort and write the same thing in spec and schema for example?

dominicm09:06:36

@imre: I agree. I've been working on a library in this space, and been thinking about it in general, for some time. Spec isn't general enough for me, I think this comes about from it's insistence on encouraging some use-cases of spec to be done in a more "clojure-y" way.

imre09:06:10

hmm clojure-y might mean good and bad

imre09:06:51

being more functional is good

imre09:06:59

data over functions is good

imre09:06:15

repl-driven dev over tdd is not (heresy!)

imre09:06:35

at least in my opinion

malcolmsparks17:06:59

I agree with the points here. Lisp loves runtime. Everything else is catering to other languages' weaknesses.