10

リング アプリで例外を処理する慣用的な方法は何ですか。例外をキャプチャして 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)))
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 に答える