在Snap Handler中捕获HTTPException

时间:2016-05-02 23:47:26

标签: haskell haskell-snap-framework httpexception

在我的一个快照处理程序中,我使用http-client通过HTTP调用外部资源,这可以抛出HttpException。在最简单的情况下,我只想捕获错误并在调试时写入std;但是,我很难将这些类型排成一行。

assignCategories' :: String -> [String] -> Handler App (AuthManager App) String
assignCategories' l_id cats  = do
            let baseAttribs = M.fromList [("id",l_id)]                                                            
            let saveQuery = WMSaveObject {baseAttributesSO= baseAttribs ,relatedSO=relatedObjects}

            -- this is the function that can throw an HTTPException            
            -- runWMSave :: WMQueryObj -> Handler App (AuthManager App) String

            saveresponse <- try $ H.runWMSave saveQuery
            return $ case saveresponse of
                Left  e -> show (e :: HttpException)
                Right r -> r

我已经玩过各种类型,但是我一般会遇到如下错误...是否有最佳实践在处理程序中调用HTTP并捕获异常?

我正在使用Control.Monad.Catch。我尝试使用Control.Exception的try,但在排序类型方面遇到了更多困难。

   No instance for (Control.Monad.Catch.MonadCatch
                       (Handler App (AuthManager App)))
      arising from a use of ‘try’
    In the expression: try
    In a stmt of a 'do' block:
      saveresponse <- try $ H.runWMSave saveQuery
    In the expression:
      do { liftIO $ putStrLn l_id;
           let baseAttribs = M.fromList ...;
           let relatedObjects = concatMap makeRelatedRow cats;
           let saveQuery = ...;
           .... }

谢谢, 尼尔

1 个答案:

答案 0 :(得分:0)

问题解决了,我使用了MonadCatchIO变换器中的try并且有效。