Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Convert Exception raw-catches to predicate-style.

  • Loading branch information...
commit a435ad4ce77b741155759bc8b2a1ef03d6704d1f 1 parent 8905c81
@technomancy authored
Showing with 24 additions and 20 deletions.
  1. +12 −12 src/dce/handle.clj
  2. +12 −8 test/dce/test/exception.clj
View
24 src/dce/handle.clj
@@ -47,22 +47,22 @@
(defn- body-form? [x]
(or (not (seq? x)) (not= 'catch (first x))))
-(defn- raw? [[_ x]]
+(defn- class-catch? [x]
(and (symbol? x) (class? (resolve x))))
(defmacro try+
[& body]
(let [[tried catches] (partition-by body-form? body)
- [raw-catches dce-catches] (separate raw? catches)
exception (gensym)]
`(try
- (try
- ~@tried
- (catch dce.Exception ~exception
- (cond
- ~@(mapcat
- (fn [[_ pred local & body]]
- `[(~pred ~exception) (let [~local ~exception] ~@body)])
- dce-catches)
- :else (throw ~exception))))
- ~@raw-catches)))
+ ~@tried
+ (catch Throwable ~exception
+ (cond
+ ~@(mapcat
+ (fn [[_ pred local & body]]
+ (let [pred (if (class-catch? pred)
+ `(fn [x#] (instance? ~pred x#))
+ pred)]
+ `[(~pred ~exception) (let [~local ~exception] ~@body)]))
+ catches)
+ :else (throw ~exception))))))
View
20 test/dce/test/exception.clj
@@ -48,6 +48,8 @@
(handle :funky {:keys [funkiness]}
funkiness)))))
+(defn funky? [x])
+
(deftest test-try+
(is (= :lots (try+
(throw+ :message "duuuude" :funky true :funkiness :lots)
@@ -58,11 +60,13 @@
(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 _
- :blowhard)))))
+ (throw+ (Exception. "whoops"))
+ (catch :funky {:keys [funkiness]}
+ funkiness)
+ (catch #(re-find #"s3" (.getMessage %)) e
+ :s3-sucks!)
+ (catch :non-funky _
+ :bummer-dude)
+ (catch funky? x)
+ (catch Exception _
+ :blowhard)))))
Please sign in to comment.
Something went wrong with that request. Please try again.