Exception 带aeson/ATOPASSEREC的导管,一旦源没有更多数据,如何毫无例外地干净退出

Exception 带aeson/ATOPASSEREC的导管,一旦源没有更多数据,如何毫无例外地干净退出,exception,haskell,conduit,aeson,attoparsec,Exception,Haskell,Conduit,Aeson,Attoparsec,我正在使用aeson/attoparsec和conduct/conduct http通过conduct attoparsec连接来解析文件/Web服务器中的JSON数据。我的问题是我的管道总是抛出这个异常 ParseError {errorContexts = ["demandInput"], errorMessage = "not enough bytes", errorPosition = 1:1} …一旦插座关闭或我们碰到EOF。解析并通过管道等传递生成的数据结构效果很好,但总是以sin

我正在使用
aeson
/
attoparsec
conduct
/
conduct http
通过
conduct attoparsec
连接来解析文件/Web服务器中的JSON数据。我的问题是我的管道总是抛出这个异常

ParseError {errorContexts = ["demandInput"], errorMessage = "not enough bytes", errorPosition = 1:1}
…一旦插座关闭或我们碰到EOF。解析并通过管道等传递生成的数据结构效果很好,但总是以
sinkParser
抛出此异常而结束。我这样调用它

j <- CA.sinkParser json
产出:

out: ParseError {errorContexts = ["demandInput"], errorMessage = "not enough bytes", errorPosition = 3:1}
和out.txt:

MyMessage "abc"MyMessage "123"

这是
conduitParserEither
的完美用例:

parseMessage :: (MonadIO m, MonadResource m) => Conduit B.ByteString m B.ByteString
parseMessage =
    CA.conduitParserEither json =$= awaitForever go
  where
    go (Left s) = error $ show s
    go (Right (_, msg)) = yield $ B8.pack $ show msg ++ "\n"

如果您在FP Haskell Center,您可以。

也许我只是对attoparsec有一个误解,但即使我有一个很好的方法来处理此异常,如何正确解释它?在我的例子中,没有实际的错误,一切都很好,我只想停止解析。但是这个错误看起来也可能出现在数据损坏时,JSON数据正好在中间结束。每个attoparsec管道是否都以异常结束,我只需查看异常中的错误字符串,以确定这是实际错误还是仅仅是EOF?错误消息是准确的。它所做的是正确解析JSON的前两个片段,第三个发现是没有可用的数据,因此说存在解析错误。您可能想看看conduitParserEither的实现,它显式地检查EOF。好的,谢谢您的澄清!我想我会同意你的建议,使用conduitParserEither来检测“demandInput”错误,并重新抛出其他所有内容,以实现实际的错误处理代码。
MyMessage "abc"MyMessage "123"
parseMessage :: (MonadIO m, MonadResource m) => Conduit B.ByteString m B.ByteString
parseMessage =
    CA.conduitParserEither json =$= awaitForever go
  where
    go (Left s) = error $ show s
    go (Right (_, msg)) = yield $ B8.pack $ show msg ++ "\n"