我正在使用 happstack-lite 编写 Web 应用程序。当我的 ServerPart 响应处理程序调用纯函数并且此函数调用error时,整个处理程序失败并且 happstack 打印默认错误消息。
handler :: ServerPart Response
handler = do
head [] `seq` ok $ toResponse $ H.html "mymessage"
服务器错误:Prelude.head:空列表
我想知道是否有可能捕获此异常并返回自定义消息。不幸的是,我不知道如何在 ServerPart monad中使用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))