习惯的方式捕捉环形应用程序中的例外 [英] idiomatic way to catch exceptions in ring apps
本文介绍了习惯的方式捕捉环形应用程序中的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在环形应用程式中处理例外的惯用方式是什么?我想捕获异常并返回一个500页。
我使用的是下面的代码,但它不工作 -
(def my-app(try
/ pre>
(app
(wrap-logger true)
wrap-keyword-params
wrap-params
wrap-file-info
(wrap-fileresources / public /)
[](index-route @ prev-h nil)
[getContent] fetch-url)
[about]我们是非常酷的人!
[&]( - >没有找到 b)(
(app
[&]( - >这是一个错误响应(状态500)不断)))
解决方案您不想将整个应用程序包装在try-catch块中,的每个请求单独。这是很容易做一个中间件,这样做。例如:
(defn wrap-exception [f]
(fn [request]
(f request)
(catch Exception e
{:status 500
:bodyException caught}))))
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)))
解决方案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 [f] (fn [request] (try (f request) (catch Exception e {:status 500 :body "Exception caught"}))))
这篇关于习惯的方式捕捉环形应用程序中的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文