···1313it's `try` but a little nicer:
14141515```clojure
1616-(require '[noahtheduke.sinker :refer [try+]])
1616+(require '[noahtheduke.sinker :as sinker :refer [try+]])
17171818;; works like normal try
1919+(try+)
2020+;; => nil
2121+1922(try+ 1 2 3)
2023;; => 3
2124···2427 (ex-message ex)))
2528;; => "hello world!"
26292727-;; ex-infos with `:type` ex-data can be caught with a keyword.
3030+;; ex-infos with `:noahtheduke.sinker/type` (or `:type`) ex-data can be caught with a keyword.
2831;; the keywords are compared with isa? to respect heirarchies.
2932;; the bound variable is the ex-data, not the exception itself.
3030-(try+ (throw (ex-info "Wrong parameter" {:type :invalid-parameter
3333+(try+ (throw (ex-info "Wrong parameter" {::sinker/type :invalid-parameter
3134 :expected :abc
3235 :given :foobar}))
3336 (catch :invalid-parameter data
···3538;; => :foobar
36393740;; the exception is on the metadata of the bind under the key `:noahtheduke.sinker/exception`.
3838-(try+ (throw (ex-info "Wrong parameter" {:type :invalid-parameter
4141+(try+ (throw (ex-info "Wrong parameter" {::sinker/type :invalid-parameter
3942 :expected 'abc
4043 :given 'foobar}))
4144 (catch :invalid-parameter data
···4346;; => "Wrong parameter"
44474548;; because the ex-data is a map, it can be destructured
4646-(try+ (throw (ex-info "Wrong parameter" {:type :invalid-parameter
4949+(try+ (throw (ex-info "Wrong parameter" {::sinker/type :invalid-parameter
4750 :expected :abc
4851 :given :foobar}))
4952 (catch :invalid-parameter {:keys [expected given]}
···5659(defn pred [data]
5760 (= :value (:key data)))
58615959-(try+ (throw (ex-info "KV pair" {:type :incorrect-argument
6262+(try+ (throw (ex-info "KV pair" {::sinker/type :incorrect-argument
6063 :key :value}))
6161- (catch pred data
6262- (:key data)))
6464+ (catch pred {k :key}
6565+ k))
6366;; => :value
64676568;; like normal try, each catch is checked in definition order,
6669;; and finally clauses gotta come last
6770(defn pred2 [data]
6871 (= :value2 (:key2 data)))
6969-(def errored? (atom nil))
7272+7373+(def finally-ran? (atom nil))
70747175(try+ (assert (= 1 2) "This will work")
7276 (catch :invalid-argument _
···8084 (catch Throwable t
8185 (str "Received a " (.getName (class t))))
8286 (finally
8383- (reset! errored? "hoodee hoodee hoo")))
8787+ (reset! finally-ran? "hoodee hoodee hoo")))
8488;; => "Received a java.lang.AssertionError"
85898686-@errored?
9090+@finally-ran?
8791;; => "hoodee hoodee hoo"
8892```
9393+9494+## others in the space
9595+9696+- [exoscale/ex](https://github.com/exoscale/ex)
9797+- [scgilardi/slingshot](https://github.com/scgilardi/slingshot/)
9898+9999+this library is quite similar to `exoscale/ex`, but `ex` is solely focused on exception infos and does a lot more, with a stronger emphasis on a specific pattern of error handling. i wrote this to fill a gap in [lazytest](https://github.com/NoahTheDuke/lazytest) and to satisfy my curiosity. i don't expect this to receive widespread adoption nor do i really want it. sometimes it's just nice to make something and let others check it out, you know?
8910090101## license
91102