Skip to content

Commit

Permalink
Added try-map form and made try+ expand to try and try-map
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasmus Svensson committed Jan 11, 2011
1 parent edeaa71 commit f304814
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ This lib is available on Clojars: [map-exception "1.0.0-SNAPSHOT"]
(finally
(do-something)))

;; The above expands to:

(try
(try-map
(throwing-code)
(catch ::foo-error m
(str "got a foo error: " (:message m)))
(catch ::bar-error m
(str "got a bar error: " (:message m))))
(catch RuntimeException e
(str "got a runtime exception: " (.getMessage e)))
(catch Exception e
(str "got an exception: " (.getMessage e)))
(finally
(do-something)))

(defn other-throwing-code []
(throw-map {:message "Some message.", :a 1, :b 2, :c 3}))

Expand Down
32 changes: 25 additions & 7 deletions src/clj/se/raek/map_exception.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@



(declare analyze-body analyze-multi-body
(declare analyze-try+-body analyze-try-map-body analyze-try-multi-body
try+-error-message try-multi-error-message
expr? catch-clause? host-type?
map-catch-clause? host-catch-clause? finally-clause?)

(defmacro try+
(defmacro try-map
{:arglists ['(expr* map-catch-clause* host-catch-claus* finally-clause?)]}
[& body]
(if-let [[exprs map-catch-clauses host-catch-clauses finally-clause] (analyze-body body)]
(if-let [[exprs map-catch-clauses finally-clause]
(analyze-try-map-body body)]
`(try
~@exprs
~@(when (seq map-catch-clauses)
Expand All @@ -26,14 +27,27 @@
~@clause-body)])
map-catch-clauses)
(throw e#))))]))
~@finally-clause)
(throw (Exception. try-map-error-message))))

(defmacro try+
{:arglists ['(expr* map-catch-clause* host-catch-claus* finally-clause?)]}
[& body]
(if-let [[exprs map-catch-clauses host-catch-clauses finally-clause]
(analyze-try+-body body)]
`(try
(try-map
~@exprs
~@map-catch-clauses)
~@host-catch-clauses
~@finally-clause)
(throw (Exception. try+-error-message))))

(defmacro try-multi
{:arglists ['(dispatch-fn expr* catch-clause* finaly-clause?)]}
[dispatch-fn & body]
(if-let [[exprs catch-clauses finally-clause] (analyze-multi-body body)]
(if-let [[exprs catch-clauses finally-clause]
(analyze-try-multi-body body)]
`(try
~@exprs
~@(when (seq catch-clauses)
Expand All @@ -53,7 +67,8 @@
(defmacro try-multi-hierarchy
{:arglists ['(dispatch-fn hierarchy expr* catch-clause* finaly-clause?)]}
[dispatch-fn hierarchy & body]
(if-let [[exprs catch-clauses finally-clause] (analyze-multi-body body)]
(if-let [[exprs catch-clauses finally-clause]
(analyze-try-map-body body)]
`(try
~@exprs
~@(when (seq catch-clauses)
Expand All @@ -70,15 +85,15 @@
~@finally-clause)
(throw (Exception. try-multi-error-message))))

(defn- analyze-body [body]
(defn- analyze-try+-body [body]
(let [[exprs body2] (split-with expr? body)
[map-catch-clauses body3] (split-with map-catch-clause? body2)
[host-catch-clauses body4] (split-with host-catch-clause? body3)
[finally-clauses body5] (split-with finally-clause? body4)]
(when-not (or (seq body5) (next finally-clauses))
[exprs map-catch-clauses host-catch-clauses finally-clauses])))

(defn- analyze-multi-body [body]
(defn- analyze-try-map-body [body]
(let [[exprs body2] (split-with expr? body)
[catch-clauses body3] (split-with catch-clause? body2)
[finally-clauses body4] (split-with finally-clause? body3)]
Expand All @@ -88,6 +103,9 @@
(def ^{:private true} try+-error-message
"A try+ form must follow the pattern (try+ expr* map-catch-clause* host-catch-clause* finally-clause?)")

(def ^{:private true} try-map-error-message
"A try-map form must follow the pattern (try+ expr* map-catch-clause* finally-clause?)")

(def ^{:private true} try-multi-error-message
"A try-multi form must follow the pattern (try-multi dispatch-fn expr* catch-clause* finally-clause?)")

Expand Down

0 comments on commit f304814

Please sign in to comment.