|
| 1 | +(ns clj-commons.slingshot.issue31-test |
| 2 | + "See https://github.com/scgilardi/slingshot/issues/31 for context." |
| 3 | + (:require [clj-commons.slingshot :refer [try+ throw+]] |
| 4 | + [clojure.test :refer [deftest is]])) |
| 5 | + |
| 6 | +(defn stacktrace-depth |
| 7 | + ([t] (stacktrace-depth 1 t)) |
| 8 | + ([i ^Throwable t] |
| 9 | + (if (.getCause t) |
| 10 | + (recur (inc i) (.getCause t)) |
| 11 | + i))) |
| 12 | + |
| 13 | +(defn stacking-up [] |
| 14 | + (try+ |
| 15 | + (try+ |
| 16 | + (try+ |
| 17 | + (try+ |
| 18 | + (throw (RuntimeException. "Something went wrong")) |
| 19 | + (catch RuntimeException _e |
| 20 | + (throw+ {:some-info "here" |
| 21 | + :type 1} |
| 22 | + (:cause &throw-context) |
| 23 | + (:message &throw-context)))) |
| 24 | + (catch [:type 1] {:as error-stuff} |
| 25 | + (throw+ (assoc error-stuff :more-info "over there") |
| 26 | + (:cause &throw-context) |
| 27 | + (:message &throw-context)))) |
| 28 | + (catch [:type 1] {:as error-stuff} |
| 29 | + (throw+ (assoc error-stuff :one-last "thing") |
| 30 | + (:cause &throw-context) |
| 31 | + (:message &throw-context)))) |
| 32 | + (catch (constantly true) {:as err-ctx} |
| 33 | + [(stacktrace-depth (:throwable &throw-context)) |
| 34 | + (.getMessage (:throwable &throw-context)) |
| 35 | + err-ctx]))) |
| 36 | + |
| 37 | +(comment |
| 38 | + (stacking-up) |
| 39 | + ) |
| 40 | + |
| 41 | +(deftest test-stacking-up |
| 42 | + (let [[depth msg err-map] (stacking-up)] |
| 43 | + (is (= 1 depth)) |
| 44 | + (is (= "Something went wrong" msg)) |
| 45 | + (is (= {:one-last "thing" |
| 46 | + :more-info "over there" |
| 47 | + :some-info "here" |
| 48 | + :type 1} err-map)))) |
0 commit comments