Skip to content

Instantly share code, notes, and snippets.

@arohner
Created September 10, 2013 03:43
Show Gist options
  • Select an option

  • Save arohner/6504697 to your computer and use it in GitHub Desktop.

Select an option

Save arohner/6504697 to your computer and use it in GitHub Desktop.
(try+ true)
(try
true
(catch
java.lang.Throwable
&throw-context
(clojure.core/let [&throw-context (clojure.core/->
&throw-context
slingshot.support/get-context
slingshot.support/*catch-hook*)]
(clojure.core/cond
(clojure.core/contains? &throw-context :catch-hook-return) (:catch-hook-return
&throw-context)
(clojure.core/contains? &throw-context :catch-hook-throw) (slingshot.slingshot/throw+
(:catch-hook-throw
&throw-context))
(clojure.core/contains? &throw-context :catch-hook-rethrow) (slingshot.slingshot/throw+)
:else (slingshot.slingshot/throw+)))))
@arohner

arohner commented Sep 10, 2013

Copy link
Copy Markdown
Author
(def ^{:dynamic true
       :doc "Hook to allow overriding the behavior of catch. Must be
  bound to a function of one argument, a context map. Returns
  a (possibly modified) context map to be considered by catch clauses.

  Normal processing by catch clauses can be skipped by adding special
  keys to the context map:

  If the context contains the key:
    - :catch-hook-return, try+ will return the corresponding value;
    - :catch-hook-throw, try+ will throw+ the corresponding value;
    - :catch-hook-rethrow, try+ will rethrow the caught object's
      outermost wrapper.

  Defaults to identity."}
  *catch-hook* identity)

@arohner

arohner commented Sep 10, 2013

Copy link
Copy Markdown
Author

expansion for

(try+
   true
   (catch bar t
     (t/print-env "throw-context")
     false))
(try
  true
  (catch
    java.lang.Throwable
    &throw-context
    (clojure.core/let [&throw-context (clojure.core/->
                                       &throw-context
                                       slingshot.support/get-context
                                       slingshot.support/*catch-hook*)]
      (clojure.core/cond
        (clojure.core/contains? &throw-context :catch-hook-return) (:catch-hook-return
                                                                     &throw-context)
        (clojure.core/contains? &throw-context :catch-hook-throw) (slingshot.slingshot/throw+
                                                                    (:catch-hook-throw
                                                                      &throw-context))
        (clojure.core/contains? &throw-context :catch-hook-rethrow) (slingshot.slingshot/throw+)
        (bar (:object &throw-context)) (clojure.core/let 
                                         [t (:object &throw-context)]
                                         (t/print-env "throw-context")
                                         false)
        :else (slingshot.slingshot/throw+)))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment