idiomatic way to catch exceptions in ring apps

僤鯓⒐⒋嵵緔 提交于 2019-12-05 20:16:05

问题


What is the idiomatic way to handle exceptions in ring apps. I would like to capture the exception and return a 500 page. How do I do that ?

I am using moustache for the code below, however it doesnt work -

(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)))

回答1:


You don't want to wrap the whole app in a try-catch block, you want to wrap the handling of each request separately. It's quite easy to make a middleware that does this. Something like:

(defn wrap-exception [handler]
  (fn [request]
    (try (handler request)
      (catch Exception e
         {:status 500
          :body "Exception caught"}))))


来源:https://stackoverflow.com/questions/12627410/idiomatic-way-to-catch-exceptions-in-ring-apps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!