在环形应用中处理异常的惯用方法是什么?我想捕获异常并返回500页。我该怎么做?
我在下面的代码中使用小胡子,但它不起作用 -
(def my-app (try
(app
(wrap-logger true)
wrap-keyword-params
wrap-params
wrap-file-info
(wrap-file "resources/public/")
[""] (index-route @prev-h nil)
["getContent"] (fetch-url)
["about"] "We are freaking cool man !!"
[&] (-> "Nothing was found" response (status 404) constantly))
(catch Exception e
(app
[&] (-> "This is an error" response (status 500) constantly)))
答案 0 :(得分:27)
您不希望将整个应用程序包装在try-catch块中,您希望单独处理每个请求。制作一个可以做到这一点的中间件非常容易。类似的东西:
(defn wrap-exception [handler]
(fn [request]
(try (handler request)
(catch Exception e
{:status 500
:body "Exception caught"}))))