处理环形应用程序中的异常的惯用方法是什么?我想捕获异常并返回 500 页。我怎么做 ?
我在下面的代码中使用了 Mustache,但是它不起作用 -
(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)))
您不想将整个应用程序包装在 try-catch 块中,而是希望单独包装每个请求的处理。制作一个执行此操作的中间件非常容易。就像是:
(defn wrap-exception [handler]
(fn [request]
(try (handler request)
(catch Exception e
{:status 500
:body "Exception caught"}))))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)