-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ring.util.async/raising function
Closes #403.
- Loading branch information
1 parent
16c1a56
commit f7e66aa
Showing
2 changed files
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) |