[mod] Remove advanced options from catch->error!

`catch->error!` with default opts is quite handy for use with `trace!`/`spy!`.

But there's a lot that users might want to customize, including:

  - Exactly what error type to catch.
  - Whether or not to rethrow on catch.
  - Error binding sym to enable use within signal message, data, etc.

We could support all of this via `catch->error!` opts but there's not much
point. If anyway customizing such behaviour, it'd be better for the user to just
use an appropriate `try/catch`.

So I've now documented this recommendation, and removed all but the most basic
(:catch-val) options.

This is a BREAKING change for anyone that was previously using any of the
following options:

  :rethrow?
  :catch-sym

Note that `:rethrow?` was never particularly helpful (independently of
`:catch-val` anyway), and the removal of `:catch-sym` will throw a compile-time
error for any existing users.
This commit is contained in:
Peter Taoussanis 2024-12-22 14:13:31 +01:00
parent d2386d62f1
commit 0de5c094e5
5 changed files with 22 additions and 31 deletions

View file

@ -169,9 +169,8 @@
(t/catch->error! (t/catch->error!
{:let [x "x"] ; Available to `:data` and `:msg` {:let [x "x"] ; Available to `:data` and `:msg`
:data {:x x} :data {:x x}
:msg ["My msg:" x my-error] :msg ["My msg:" x]
:catch-val "Return value when form throws" :catch-val "Return value when form throws"}
:catch-sym my-error}
(/ 1 0))) (/ 1 0)))
;;;; Wiki examples ;;;; Wiki examples

View file

@ -1,12 +1,15 @@
ALWAYS (unconditionally) executes given `run` form and: ALWAYS (unconditionally) executes given `run` form and:
If `run` form succeeds: return the form's result. If `run` form succeeds: returns the form's result.
If `run` form throws: If `run` form throws ANYTHING:
Call `error!` with the thrown error and the given signal options [2], Calls `error!` with the thrown error and given signal options [2], then
then return (:catch-val opts) if it exists, or rethrow the error. either returns given (:catch-val opts), or rethrows.
Default kind: `:error` Default kind: `:error`
Default level: `:error` Default level: `:error`
Just a convenience util. For more flexibility use your own `try/catch`.
See `taoensso.encore/try*` for easily catching cross-platform errors.
Examples: Examples:
(catch->error! (/ 1 0)) ; %> {:kind :error, :level :error, :error <caught> ...} (catch->error! (/ 1 0)) ; %> {:kind :error, :level :error, :error <caught> ...}
@ -14,12 +17,10 @@ Examples:
(catch->error! (catch->error!
{:let [x "x"] ; Available to `:data` and `:msg` {:let [x "x"] ; Available to `:data` and `:msg`
:data {:x x} :data {:x x}
:msg ["My msg:" x my-error] :msg ["My msg:" x]
:catch-val "Return value when form throws" :catch-val "Return value iff form throws"}
:catch-sym my-error ; Sym of caught error, available to `:data` and `:msg`
}
(/ 1 0)) ; %> {... :data {x "x"}, :msg_ "My msg: x <caught>" ...} (/ 1 0)) ; %> {... :data {x "x"}, :msg_ "My msg: x" ...}
Tips: Tips:

View file

@ -281,17 +281,16 @@
:arglists (impl/signal-arglists :catch->error!)} :arglists (impl/signal-arglists :catch->error!)}
([opts-or-id run] `(catch->error! ~(assoc (merge-or-assoc-opts base-opts &form &env :id opts-or-id) :run run))) ([opts-or-id run] `(catch->error! ~(assoc (merge-or-assoc-opts base-opts &form &env :id opts-or-id) :run run)))
([opts-or-run] ([opts-or-run]
(let [opts (merge-or-assoc-opts base-opts &form &env :run opts-or-run) (let [opts (merge-or-assoc-opts base-opts &form &env :run opts-or-run)
rethrow? (if (contains? opts :catch-val) false (get opts :rethrow? true)) rethrow? (not (contains? opts :catch-val))
catch-val (get opts :catch-val) catch-val (get opts :catch-val)
catch-sym (get opts :catch-sym '__caught-error) ; Undocumented run-form (get opts :run)
run-form (get opts :run) opts (dissoc opts :run :catch-val)]
opts (dissoc opts :run :catch-val :catch-sym :rethrow?)]
`(enc/try* ~run-form `(enc/try* ~run-form
(catch :all ~catch-sym (catch :all ~'__caught
(impl/signal! ~(assoc opts :error catch-sym)) (impl/signal! ~(assoc opts :error '__caught))
(if ~rethrow? (throw ~catch-sym) ~catch-val)))))))) (if ~rethrow? (throw ~'__caught) ~catch-val))))))))
(comment (with-signal (catch->error! ::my-id (/ 1 0)))) (comment (with-signal (catch->error! ::my-id (/ 1 0))))

View file

@ -444,7 +444,7 @@
'([opts-or-run] '([opts-or-run]
[id run] [id run]
[{:as opts-map :keys [{:as opts-map :keys
[#_defaults #_elide? #_allow? #_expansion-id, rethrow? catch-val, [#_defaults #_elide? #_allow? #_expansion-id, catch-val,
elidable? location #_location* inst uid middleware middleware+, elidable? location #_location* inst uid middleware middleware+,
sample-rate kind ns id level when rate-limit rate-limit-by, sample-rate kind ns id level when rate-limit rate-limit-by,
ctx ctx+ parent root trace?, do let data msg error #_run & kvs]} ctx ctx+ parent root trace?, do let data msg error #_run & kvs]}

View file

@ -627,16 +627,8 @@
(let [[[rv re] [sv]] (with-sigs (tel/catch->error! (ex1!)))] [(is (ex1? re)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id nil}))]) (let [[[rv re] [sv]] (with-sigs (tel/catch->error! (ex1!)))] [(is (ex1? re)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id nil}))])
(let [[[rv re] [sv]] (with-sigs (tel/catch->error! :id1 (ex1!)))] [(is (ex1? re)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id :id1}))]) (let [[[rv re] [sv]] (with-sigs (tel/catch->error! :id1 (ex1!)))] [(is (ex1? re)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id :id1}))])
(let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:id :id1} (ex1!)))] [(is (ex1? re)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id :id1}))]) (let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:id :id1} (ex1!)))] [(is (ex1? re)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id :id1}))])
(let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:rethrow? false} (ex1!)))] [(is (nil? re)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id nil}))])
(let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:catch-val :foo} (ex1!)))] [(is (= rv :foo)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id nil}))]) (let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:catch-val :foo} (ex1!)))] [(is (= rv :foo)) (is (sm? sv {:kind :error, :line :submap/some, :level :error, :error pex1?, :id nil}))])
(let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:catch-val :foo} (+ 1 2)))] [(is (= rv 3)) (is (nil? sv))]) (let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:catch-val :foo} (+ 1 2)))] [(is (= rv 3)) (is (nil? sv))])])
(let [[[rv re] [sv]] (with-sigs (tel/catch->error! {:catch-val :foo ; Overrides `:rethrow?`
:rethrow? true} (+ 1 2)))] [(is (= rv 3)) (is (nil? sv))])
(let [[[rv] [sv]] (with-sigs (tel/catch->error! {:catch-val nil
:catch-sym my-err
:data {:my-err my-err}} (ex1!)))]
[(is (= rv nil)) (is (sm? sv {:kind :error, :data {:my-err pex1?}}))])])
#?(:clj #?(:clj
(testing "uncaught->error!" (testing "uncaught->error!"