0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 08:50:57 -05:00

Add better exception macros (ignoring and try).

This commit is contained in:
Andrey Antukh 2020-10-20 17:23:35 +02:00 committed by Hirunatan
parent 823aa426ed
commit f57941fd7c

View file

@ -25,23 +25,24 @@
[& {:keys [type code message hint cause] :as params}] [& {:keys [type code message hint cause] :as params}]
(s/assert ::error-params params) (s/assert ::error-params params)
(let [message (or message hint "") (let [message (or message hint "")
payload (dissoc params :cause :message)] payload (dissoc params :cause)]
(ex-info message payload cause))) (ex-info message payload cause)))
(defmacro raise (defmacro raise
[& args] [& args]
`(throw (error ~@args))) `(throw (error ~@args)))
(defn ignoring* (defn try*
[f] [f on-error]
(try (try (f) (catch #?(:clj Exception :cljs :default) e (on-error e))))
(f)
(catch #?(:clj Exception :cljs :default) e
nil)))
;; http://clj-me.cgrand.net/2013/09/11/macros-closures-and-unexpected-object-retention/ ;; http://clj-me.cgrand.net/2013/09/11/macros-closures-and-unexpected-object-retention/
;; Explains the use of ^:once metadata ;; Explains the use of ^:once metadata
(defmacro ignoring (defmacro ignoring
[& exprs] [& exprs]
`(ignoring* (^:once fn* [] ~@exprs))) `(try* (^:once fn* [] ~@exprs) (constantly nil)))
(defmacro try
[& exprs]
`(try* (^:once fn* [] ~@exprs) identity))