2

我正在使用 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))
4

1 回答 1

0

据我所知,happstack 使用 monad-control,你应该能够在适当的地方使用它的提升捕获(只要你也适当地强制异常):http ://hackage.haskell.org/包/monad-control-0.3.1

于 2012-03-20T17:57:30.903 回答