Haskell 如何使用http枚举器处理异常

Haskell 如何使用http枚举器处理异常,haskell,Haskell,哈斯克尔是个新手。我试图使用http枚举器通过http上的XML查询服务。我能够连接并发送xml格式的请求,并接收xml格式的响应 当查询成功时,服务器发回一个以 我正在努力解决的是处理故障文档中指出的异常的正确方法。我试图使用或,但没有成功 我在ghci中汇编了以下内容: import Network.HTTP.Enumerator import Network.HTTP.Types import qualified Data.ByteString.Lazy as L import Da

哈斯克尔是个新手。我试图使用http枚举器通过http上的XML查询服务。我能够连接并发送xml格式的请求,并接收xml格式的响应

当查询成功时,服务器发回一个以 我正在努力解决的是处理故障文档中指出的异常的正确方法。我试图使用
,但没有成功

我在ghci中汇编了以下内容:

import Network.HTTP.Enumerator 
import Network.HTTP.Types

import qualified Data.ByteString.Lazy as L 
import Data.ByteString.UTF8

import Text.XML.Light

hostname = "https://server..."

doPost username password token = do

    let q = QName "SYSTEM" Nothing Nothing

    let attribs = [Attr {attrKey = QName "user" Nothing Nothing, attrVal = username},
                   Attr {attrKey = QName "password" Nothing Nothing, attrVal = password},
                   Attr {attrKey = QName "token" Nothing Nothing, attrVal = token}]

    let doc = Element {elName=q, elAttribs=attribs, elContent= [], elLine=Nothing}

    req0 <- parseUrl hostname 

    let req = req0 { method = methodPost 
                  , requestHeaders = [("Content-Type", "text/xml")]   
                  , requestBody = RequestBodyBS $ fromString $ showTopElement doc
                  } 

    res <- withManager $ httpLbs req 

    let status = Network.HTTP.Enumerator.statusCode res
    let content = responseBody res

    -- this is where I would check for different fault codes using a case statement
    if content == "<FAULT/>"
        then Left "error"
        else Right content
import Network.HTTP.Enumerator
导入Network.HTTP.Types
将限定的Data.ByteString.Lazy作为L导入
导入Data.ByteString.UTF8
导入Text.XML.Light
主机名=”https://server..."
doPost用户名密码令牌=do
设q=QName“系统”无
让attribs=[Attr{attrKey=QName“user”Nothing Nothing,attrVal=username},
Attr{attrKey=QName“password”Nothing Nothing,attrVal=password},
Attr{attrKey=QName“token”Nothing Nothing,attrVal=token}]
让doc=Element{elName=q,elAttribs=attribs,elContent=[],elLine=Nothing}
req0 doPost“用户”“密码”
:1:1:
没有(Control.Failure.Failure)的实例
HttpException(a0或a0),
Control.Monad.IO.Control.MonadControlIO(a0或a0))
由使用“doPost”引起的
可能的解决方案:
为添加实例声明
(Control.Failure.Failure HttpException(a0或a0),
Control.Monad.IO.Control.MonadControlIO(a0或a0))
在表达式中:doPost“user”“password”“”
在“it”的等式中:it=doPost“user”“password”
在这种情况下,处理异常的最佳方法是什么

提前谢谢。 尼尔

你需要在最后一个if之前加上一个“return$”。parseUrl需要在monad中运行,该monad是
故障HttpException
的一个实例,例如IO或Maybe
withManager
需要一个作为
MonadControl
实例的monad,例如
IO


当前,末尾的
if
正在强制整个
do
-块在
任一字符串中运行,这就是为什么会出现“无实例”异常。如果您添加一个返回,您的最终结果将类似于
IO(或字符串XML)

您需要一种方法来处理“无实例”异常吗?好的,因为这里的“无实例”错误只是一个编译错误。然后,您是否正在尝试将自定义的
“”
错误与常规的
http枚举器
错误集成?否,因为常规的http枚举器错误似乎与http问题有关(这是有意义的),而错误是应用程序级错误,而不是http连接问题。
*Main> doPost "user" "password" ""

<interactive>:1:1:
    No instances for (Control.Failure.Failure
                        HttpException (Either a0),
                      Control.Monad.IO.Control.MonadControlIO (Either a0))
      arising from a use of `doPost'
    Possible fix:
      add instance declarations for
      (Control.Failure.Failure HttpException (Either a0),
       Control.Monad.IO.Control.MonadControlIO (Either a0))
    In the expression: doPost "user" "password" ""
    In an equation for `it': it = doPost "user" "password" ""