From 500179a66f9880fd797616107e7f6852f8ffa110 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Mon, 30 Dec 2024 17:31:03 +0400 Subject: [PATCH] Wrap exceptions during the build process. Refs #39 --- src/darkleaf/di/core.clj | 13 +++++++++++-- test/darkleaf/di/tutorial/y_graceful_stop_test.clj | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 3da8438d..df2c317e 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -92,12 +92,21 @@ :declared-deps deps :remaining-deps (seq deps)})) -(defn- build-obj [built-map head] +(defn- build-obj* [built-map head] (let [factory (:factory head) declared-deps (:declared-deps head) built-deps (select-keys built-map (keys declared-deps))] (p/build factory built-deps))) +(defn- build-obj [built-map stack] + (try + (build-obj* built-map (peek stack)) + (catch Exception ex + (throw (ex-info "A failure occurred during the build process" + {:type ::build-failure + :stack (map :key stack)} + ex))))) + (defn- build [{:keys [registry *stop-list]} key] (loop [stack (list (stack-frame key :required (registry key))) built-map {}] @@ -126,7 +135,7 @@ built-map)) :else - (let [obj (build-obj built-map head) + (let [obj (build-obj built-map stack) stop #(p/demolish factory obj)] (vswap! *stop-list conj stop) (case [obj dep-type] diff --git a/test/darkleaf/di/tutorial/y_graceful_stop_test.clj b/test/darkleaf/di/tutorial/y_graceful_stop_test.clj index 83909ad0..fa4a0eb9 100644 --- a/test/darkleaf/di/tutorial/y_graceful_stop_test.clj +++ b/test/darkleaf/di/tutorial/y_graceful_stop_test.clj @@ -32,7 +32,7 @@ ::on-stop-dep-ex on-stop-dep-ex} ex (try (di/start `root registry) - (catch Throwable ex + (catch Exception ex ex))] - (t/is (= on-start-root-ex ex)) + (t/is (= on-start-root-ex (ex-cause ex))) (t/is (= [on-stop-dep-ex] (vec (.getSuppressed ex))))))