Haskell 捕获happstack lite处理程序中的异常
我正在使用happstack lite编写web应用程序。当我的ServerPart响应处理程序调用pure函数且此函数调用error时,整个处理程序将失败,happstack将打印默认错误消息Haskell 捕获happstack lite处理程序中的异常,haskell,happstack,Haskell,Happstack,我正在使用happstack lite编写web应用程序。当我的ServerPart响应处理程序调用pure函数且此函数调用error时,整个处理程序将失败,happstack将打印默认错误消息 handler :: ServerPart Response handler = do head [] `seq` ok $ toResponse $ H.html "mymessage" 服务器错误:Prelude.head:空列表 我想知道是否有可能捕获此异常并返回自定义消息。不幸的是,我
handler :: ServerPart Response
handler = do
head [] `seq` ok $ toResponse $ H.html "mymessage"
服务器错误:Prelude.head:空列表
我想知道是否有可能捕获此异常并返回自定义消息。不幸的是,我不知道如何在ServerPartMonad中使用catch
理想情况下,我的处理程序应该如下所示:
handler :: ServerPart Response
handler = do
catch
(head [] `seq` ok $ toResponse $ H.html "my message")
(ok $ toResponse $ H.html "my error")
更新:为历史记录保存我的解决方案
handler :: ServerPart Response
handler = do
let good = ok $ toResponse $ H.html "my message"
let bad = ok $ toResponse $ H.html "my error"
join (liftIO $ catch
(do
let n = head (tail [1])
_ <- print n
return good
)
(\(E.ErrorCall _) -> return bad))
处理程序::服务器部分响应
handler=do
让good=ok$t响应$H.html“我的消息”
让bad=ok$t响应$H.html“我的错误”
加入(liftIO$catch)
(做
设n=头(尾[1])
_返回坏消息)
据我所知,happstack使用monad控件,您应该能够在适当的位置使用其提升的捕获(只要您也适当地强制执行异常):据我所知,happstack使用monad控件,您应该能够在适当的位置使用其提升的捕获(只要您也适当地强制执行异常):