In page 224, the following code is suggested.
(defn load-resource [path]
(try
(if (forbidden? path)
(throw (ex-info "Forbidden resource"
{:status 403 :resource path}))
(slurp path))
(catch java.io.FileNotFoundException e
(throw (ex-info "Missing resource"
{:status 404 :resource path})))
(catch java.io.IOException e
(throw (ex-info "Server error"
{:status 500 :resource path})))))
I’ve seen this in practice a lot and not including the cause when catching an exception generates headaches to debug later.
I’d suggest to do a bit of explanation of adding the cause exception to the ex info in the catch clauses like this:
(defn load-resource [path]
(try
(if (forbidden? path)
(throw (ex-info "Forbidden resource"
{:status 403 :resource path}))
(slurp path))
(catch java.io.FileNotFoundException e
(throw (ex-info "Missing resource"
{:status 404 :resource path}
e)))
(catch java.io.IOException e
(throw (ex-info "Server error"
{:status 500 :resource path}
e)))))
This is really useful for me to add context to exceptions that might happen in calling functions on sequences to understand what element failed.
Here’s a dummy example where I can call a function that might throw, and I would want more context on where it failed, and I can add context to the existing ex-info.
(defn randomly-fails []
(when (> (rand-int 10) 8)
(throw (ex-info "Randomly failed!" {}))))
(run! #(try (randomly-fails)
(catch clojure.lang.ExceptionInfo e
(throw (ex-info (ex-message e)
(assoc (ex-data e) :n %)
e))))
(range 100))
=> #'examples.interop/randomly-fails
Execution error (ExceptionInfo) at examples.interop/randomly-fails (form-init16509100671821839256.clj:3).
Randomly failed!
*e
=>
#error
{:cause "Randomly failed!",
:data {},
:via [{:type clojure.lang.ExceptionInfo,
:message "Randomly failed!",
:data {:n 7},
:at [examples.interop$eval2303$fn__2304 invoke "form-init16509100671821839256.clj" 6]}
{:type clojure.lang.ExceptionInfo,
:message "Randomly failed!",
:data {},
:at [examples.interop$randomly_fails invokeStatic "form-init16509100671821839256.clj" 3]}],