Hi all, how would you fix the following mutable-like code?
(let [start (System/currentTimeMillis)
response (atom nil)]
(try
(log/info "request:" url)
(reset! response (client/get url opts))
(:body @response)
(finally
(log/info "status" (:status @response) "duration:" (- (System/currentTimeMillis) start) "ms"))))
the finally
needs to have the try
as immediate parent
so I can’t use let
and nest the finally
within the let
I'd probably use catch
clause and log "error" there and moved "info" log to the "happy path" flow.
The log would be somewhat duplicated but I guess you can create a little function to cover that.
I see
I was wondering if I could not use an atom, but I can’t really see how
(let [start (System/currentTimeMillis)
_ (log/info "request:" url)
response (try (client/get url opts) (catch Exception e …))]
(log/info "status" (:status response) "duration:" (- (System/currentTimeMillis) start) "ms")
(:body response))
In the ...
of the catch
you can log errors and/or return a response map with appropriate :status
and/or :body
fields.
^ @mping How does that look to you?
looks good; I would prefer to log the status and time at the same time so I dont have to use some correlation on the logs
but I see your point
That does log the status and time together.
(or are you concerned that the ...
error handling will take enough that that it would affect the millisecond timings?)