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