Skip to content

Commit

Permalink
Add ring.util.async/raising function
Browse files Browse the repository at this point in the history
Closes #403.
  • Loading branch information
weavejester committed Mar 9, 2024
1 parent 16c1a56 commit f7e66aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ring-core/src/ring/util/async.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns ring.util.async)

(defn raising
"Alter function f to catch all Throwable errors and pass them to the raise
function. Designed for use with asynchronous handlers, particularly in
wrapping a respond argument to a 3-arity handler."
{:added "1.12"}
[raise f]
(fn [& args]
(try
(apply f args)
(catch Throwable t (raise t)))))
9 changes: 9 additions & 0 deletions ring-core/test/ring/util/test/async.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns ring.util.test.async
(:require [clojure.test :refer [deftest is]]
[ring.util.async :refer [raising]]))

(deftest test-raising
(let [error (promise)]
((raising error #(/ 1 0)))
(is (realized? error))
(is (instance? ArithmeticException (deref error 0 nil)))))

0 comments on commit f7e66aa

Please sign in to comment.