10

在环形应用程序中处理异常的惯用方法是什么。我想捕获异常并返回 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)))
4

1 回答 1

29

您不想将整个应用程序包装在 try-catch 块中,而是希望分别包装每个请求的处理。制作一个这样做的中间件非常容易。就像是:

(defn wrap-exception [handler]
  (fn [request]
    (try (handler request)
      (catch Exception e
         {:status 500
          :body "Exception caught"}))))
于 2012-09-27T18:22:23.333 回答