Skip to content

Commit 656a4b4

Browse files
committed
Make wrap-exceptions robust against nil response
This happens when the connection is destroyed (via `:aleph/close true`). Without this patch, we'd always leak a deferred here.
1 parent 9cd84e8 commit 656a4b4

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/aleph/http/client_middleware.clj

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -261,35 +261,36 @@
261261
[client]
262262
(fn [req]
263263
(d/let-flow' [{:keys [status body] :as rsp} (client req)]
264-
(if (unexceptional-status? status)
265-
rsp
266-
(cond
267-
268-
(false? (opt req :throw-exceptions))
264+
(when (some? rsp) ; will happen when `:aleph/close true` is set in req
265+
(if (unexceptional-status? status)
269266
rsp
267+
(cond
268+
269+
(false? (opt req :throw-exceptions))
270+
rsp
270271

271-
(instance? InputStream body)
272-
(d/chain' (d/future (bs/to-byte-array body))
273-
(fn [body]
274-
(d/error-deferred
275-
(ex-info
272+
(instance? InputStream body)
273+
(d/chain' (d/future (bs/to-byte-array body))
274+
(fn [body]
275+
(d/error-deferred
276+
(ex-info
276277
(str "status: " status)
277278
(assoc rsp :body (ByteArrayInputStream. body))))))
278279

279-
(nil? body)
280-
(d/error-deferred
281-
(ex-info
282-
(str "status: " status)
283-
rsp))
284-
285-
:else
286-
(d/chain'
287-
(s/reduce conj [] body)
288-
(fn [body]
289-
(d/error-deferred
280+
(nil? body)
281+
(d/error-deferred
282+
(ex-info
283+
(str "status: " status)
284+
rsp))
285+
286+
:else
287+
(d/chain'
288+
(s/reduce conj [] body)
289+
(fn [body]
290+
(d/error-deferred
290291
(ex-info
291-
(str "status: " status)
292-
(assoc rsp :body (s/->source body)))))))))))
292+
(str "status: " status)
293+
(assoc rsp :body (s/->source body))))))))))))
293294

294295
(defn wrap-method
295296
"Middleware converting the :method option into the :request-method option"

0 commit comments

Comments
 (0)