2012-03-20 112 views
2

我正在使用happstack-lite編寫web應用程序。當我的ServerPart Response處理程序調用純函數並且此函數調用錯誤時,整個處理程序失敗,並且happstack輸出默認錯誤消息。happstack-lite處理程序捕獲異常

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)) 

回答

相關問題