无法推断(CI).MonadCatchIO (Handler App (AuthManager App)))在Snap



我在处理程序中使用基于openapi的库;不幸的是,它抛出了很多网络或http异常,所以我试图使用Control.Monad.CatchIO与它,但撞我的头与类型。

这是我在哪里:

{-# LANGUAGE ScopedTypeVariables #-}
import qualified Control.Exception as E
import qualified Control.Monad.CatchIO as CI (try,MonadCatchIO)
import           Control.Monad.State
import           Snap
import           Application
import           Services
import qualified Helper.Heist as H
import           Snap.AzureAdAuth
-- this is the function that throws the exceptions
runQuery :: (Produces req accept, MimeUnrender accept b1, Show b1, MimeType contentType) => ServicesRequest req contentType b1 accept -> Handler App (AuthManager App) (Either MimeError b1)
runQuery r = ...

runQuery' :: forall req accept contentType b1. (Produces req accept, MimeUnrender accept b1, Show b1, MimeType contentType)
=> ServicesRequest req contentType b1 accept
-> Handler App (AuthManager App) (Either E.SomeException (Either MimeError b1))
runQuery' req = 
CI.try $ runQuery req :: Handler App (AuthManager App) (Either E.SomeException (Either MimeError b1))

编译得到以下错误信息:

* Could not deduce (CI.MonadCatchIO
(Handler App (AuthManager App)))
arising from a use of `CI.try'
from the context: (Produces req accept, MimeUnrender accept b1,
Show b1, MimeType contentType)
bound by the type signature for:
runQuery' :: forall req accept contentType b1.
(Produces req accept, MimeUnrender accept b1, Show b1,
MimeType contentType) =>
ServicesRequest req contentType b1 accept
-> Handler
App
(AuthManager App)
(Either E.SomeException (Either MimeError b1))
at src/Helper/API.hs:(96,1)-(99,102)
* In the expression: CI.try $ runQuery req
In an equation for runQuery':
runQuery' req = CI.try $ runQuery req

对如何排列类型有什么想法吗?

我不能完全复制你的例子,因为仍然有许多类型,我不知道他们来自哪里。

Snap框架的Handlermonad使用MonadBaseControl嵌入IOmonad,而不是MonadCatchIO-mtl使用的更传统的mtl风格的MonadIOMonadCatchIO

相反,lifted-base包为这些嵌入提供了诸如try(在Control.Exception.Lifted中)之类的原语。

以下代码为我编译:

import Snap.Snaplet
import Control.Exception.Lifted hiding (Handler)
runQuery :: Handler a b c
runQuery = undefined
runQuery' :: (Exception e) => Handler a b (Either e a1)
runQuery' = try runQuery

Haskelling快乐!

最新更新