From f57941fd7c0e6fec12e1ff3d5758b5d8008da678 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 20 Oct 2020 17:23:35 +0200 Subject: [PATCH] :sparkles: Add better exception macros (ignoring and try). --- common/app/common/exceptions.cljc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/common/app/common/exceptions.cljc b/common/app/common/exceptions.cljc index 2ff5b73cc..2abd01426 100644 --- a/common/app/common/exceptions.cljc +++ b/common/app/common/exceptions.cljc @@ -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))