简体   繁体   English

在快照处理程序中捕获HTTPException

[英]Catch HTTPException within Snap Handler

Within one of my snap handlers I am calling an external resource over HTTP using http-client, which can throw an HttpException . 在我的一个快照处理程序中,我正在使用http-client通过HTTP调用外部资源,这可能引发HttpException In the simplest case, I just want to catch the error and write to std out while I debug; 在最简单的情况下,我只想捕捉错误并在调试时写出std; however, I am having a tough time getting the types lined up. 但是,我很难安排类型。

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

I've played around with the types, but am generally getting errors like below... is there a best practice for calling HTTP within a handler and catching exceptions? 我玩过这些类型,但是通常会遇到如下错误……是否存在在处理程序中调用HTTP并捕获异常的最佳实践?

I'm using Control.Monad.Catch. 我正在使用Control.Monad.Catch。 I tried using Control.Exception's try , but had even more difficulties getting types lined up. 我尝试使用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 = ...;
           .... }

Thanks, Neil 谢谢,尼尔

问题解决了,我使用了MonadCatchIO-transformers的try并成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM