My understanding is that if I use Jetty/Ring, and an Exception happens in the handler, then the Exception will be sent to the browser, unless I wrap the handler in a try/catch block.
So I have a handler like this (with some code removed because it is unimportant here):
(defn handler [request]
(pprint request)
(try
(when (System/getenv "path_to_log")
(spit (System/getenv "path_to_log") (str (get request :body) \newline \newline \newline) :append true))
(if (not (map? (get request :body)))
{:status 200
:headers {"Content-Type" "text/html"}
:body (slurp (System/getenv "path_to_index"))}
(let [
headers (get request :headers)
session-id (get headers "authorization")
params (get request :body)
choice (get params :choice)
;;; some code removed
params (sanitized-phone params)
]
(if (nil? choice)
{:status 200
:headers {"Content-Type" "text/html"}
:body (slurp (System/getenv "path_to_index"))}
(if (and
(nil? verified)
(not (= choice "get-session-id")))
(handler-response {:message (str "You are missing your session-id or the session-id has expired or you forgot to login." params )})
(handler-response (choices/choices params))))))
(catch Exception e
(log/elog e)
(handler-response {:message "Error: our engineers are working to fix this."}))
(finally
{:message "Error: our engineers are working to fix this."})))
And then I feed the handler to Jetty:
(defn initiate
[]
(try
(jetty/run-jetty
(wrap-json-body handler {:keywords? true :bigdecimals? true})
{:port 7001
:join? false
:max-threads 200
:min-threads 20
})
(catch Exception e
(log/elog e))))
As you can see, almost all the real action happens inside of here:
(handler-response (choices/choices params))
“choices” is a multi method that does different things depending on what is in the params.
I had assumed that if there was an Exception in choices
then it would never make it to the browser, because I’ve wrapped the handler code in a try/catch block. And yet, some of the choices do make it to the browser, until I wrap the individual choice
in its own try/catch block.
So did I completely misunderstand this?
Do I have to wrap every function in a try/catch block, to ensure the Exceptions happening in the function do not make it to the browser?