From cafb7abb53c23de7d6104ac010205142a265d309 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 3 Jul 2025 14:51:54 +0200 Subject: [PATCH] :tada: Add better syntax facility for `ex/try!` macro --- common/src/app/common/exceptions.cljc | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/common/src/app/common/exceptions.cljc b/common/src/app/common/exceptions.cljc index 5cceeb7222..4bb36461ea 100644 --- a/common/src/app/common/exceptions.cljc +++ b/common/src/app/common/exceptions.cljc @@ -47,10 +47,26 @@ `(try ~@exprs (catch Throwable e# nil)))) (defmacro try! - [& exprs] - (if (:ns &env) - `(try ~@exprs (catch :default e# e#)) - `(try ~@exprs (catch Throwable e# e#)))) + [expr & {:keys [reraise-with on-exception]}] + (let [ex-sym + (gensym "exc") + + generate-catch + (fn [] + (cond + (map? reraise-with) + `(ex/raise ~@(mapcat identity reraise-with) :cause ~ex-sym) + + on-exception + `(let [handler# ~on-exception] + (handler# ~ex-sym)) + + :else + ex-sym))] + + (if (:ns &env) + `(try ~expr (catch :default ~ex-sym ~(generate-catch))) + `(try ~expr (catch Throwable ~ex-sym ~(generate-catch)))))) (defn ex-info? [v]