Skip to content

Commit

Permalink
Wrap exceptions during the build process. Refs #39
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Dec 30, 2024
1 parent 75142df commit 500179a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}]
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions test/darkleaf/di/tutorial/y_graceful_stop_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))

0 comments on commit 500179a

Please sign in to comment.