Permalink
Browse files

Add throw+ to work with Exceptions or maps.

  • Loading branch information...
1 parent 608c322 commit 8905c81970fb55bb0eb248c51cc5db8d362a2729 @technomancy committed Jun 11, 2011
Showing with 36 additions and 17 deletions.
  1. +15 −9 README.md
  2. +8 −2 src/dce/Exception.clj
  3. +13 −6 test/dce/test/exception.clj
View
24 README.md
@@ -4,22 +4,28 @@ An exception for carrying data. Based on clojure.contrib.condition.
Differs from c.c.condition in a few ways:
-* the exception is a descendant of RuntimeException rather than direct Throwable
-* the exception may be treated as a Clojure map itself (rather than its metadata)
-* catch clauses may be interspersed with handle causes.
+* the exception is a descendant of RuntimeException rather than direct Throwable.
+* the exception may be treated as a Clojure map itself.
+
+Also introduces new awesomeness in the form of try+ and throw+. throw+
+takes an Exception, a map, or varargs, and constructs a data-conveying
+Exception in the latter two cases. try+ can destructure data-conveying
+exceptions in catches.
## Usage
(defn asplode [problem type]
- (dce.Exception/toss :message (str "Oh no! " problem") :type type))
+ (dce.Exception/throw+ :message (str "Oh no! " problem) :failure true))
- (handler-case :type
+ (try+
(when-not (success?)
- (asplode "failed!" :failure))
- (handle :failure e
+ (asplode "failed!"))
+ (catch :failure e
(log/warn e "stuff failed, dude: " (:message e)))
- (handle :catastrophic-failure e
- (System/exit (:exit-code e))))
+ (catch :catastrophic-failure {:keys [exit-code]}
+ (System/exit exit-code))
+ (catch java.io.IOException e
+ (log/info "whatever; who cares.")))
## License
View
10 src/dce/Exception.clj
@@ -70,5 +70,11 @@
(defn -count [self]
(count (.state self)))
-(defn toss [& {:as args}]
- (throw (dce.Exception. args)))
+(defn throw+
+ ([x]
+ (throw
+ (if (instance? Throwable x)
+ x
+ (dce.Exception. x))))
+ ([k v & kvs]
+ (throw+ (apply hash-map k v kvs))))
View
19 test/dce/test/exception.clj
@@ -1,7 +1,7 @@
(ns dce.test.exception
(:use [clojure.test]
[dce.handle]
- [dce.Exception :only [toss]]))
+ [dce.Exception :only [throw+]]))
(def e (dce.Exception. {:a :bob :b :alice}))
@@ -43,19 +43,26 @@
(deftest test-handler-case
(is (= :lots (handler-case :type
- (toss :message "duuuude" :type :funky :funkiness :lots)
+ (throw+ :message "duuuude" :type :funky :funkiness :lots)
(handle :non-funky _)
(handle :funky {:keys [funkiness]}
funkiness)))))
-(defn funky? [])
-
(deftest test-try+
(is (= :lots (try+
- (toss :message "duuuude" :funky true :funkiness :lots)
+ (throw+ :message "duuuude" :funky true :funkiness :lots)
+ (catch :funky {:keys [funkiness]}
+ funkiness)
+ (catch :non-funky _
+ :bummer-dude)
+ (catch funky? x)
+ (catch Exception _))))
+ (is (= :blowhard (try+
+ (throw+ (Exception. "whoops"))
(catch :funky {:keys [funkiness]}
funkiness)
(catch :non-funky _
:bummer-dude)
(catch funky? x)
- (catch Exception _)))))
+ (catch Exception _
+ :blowhard)))))

0 comments on commit 8905c81

Please sign in to comment.