我已经定义了一个自定义错误类型:
data Failure = NetworkError Message |
UserIsTooStupid Message |
InvalidOperation Message |
UnexpectedError Message
type Message = String
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用MonadError我的错误类型:
loadJSON :: (Aeson.FromJSON v, MonadIO m, MonadError Failure m) => URI -> m v
loadJSON uri = do
body <- liftIO $ getResponseBody =<< simpleHTTP (getRequest uri)
case Aeson.decode body of
Just json -> return json
Nothing -> throwError $ SerialisationError "Couldn't deserialise from JSON"
type URI = String
Run Code Online (Sandbox Code Playgroud)
换句话说,这个函数可以返回它同时满足任何单子MonadIO和MonadError,但错误的它可以抛出的唯一类型Failure.
这无法编译,错误消息:
Non …Run Code Online (Sandbox Code Playgroud)