在快照处理程序中捕获 HTTPException
Catch HTTPException within Snap Handler
在我的一个 snap 处理程序中,我使用 http-client 通过 HTTP 调用外部资源,它可以抛出 HttpException
。在最简单的情况下,我只想在调试时捕获错误并写入标准输出;然而,我很难把类型排好。
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 = ...;
.... }
谢谢,
尼尔
问题已解决,我使用了来自 MonadCatchIO-transformers 的 try
并且有效。
在我的一个 snap 处理程序中,我使用 http-client 通过 HTTP 调用外部资源,它可以抛出 HttpException
。在最简单的情况下,我只想在调试时捕获错误并写入标准输出;然而,我很难把类型排好。
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 = ...;
.... }
谢谢, 尼尔
问题已解决,我使用了来自 MonadCatchIO-transformers 的 try
并且有效。