执行HTTP-Conduit版本2.0.0.4的示例代码时,我该如何解决错误



我是Haskell的新手,正在尝试使用HTTP-Conduit版本2.0.0.4的示例代码,但剂量不起作用

这是示例代码

{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Conduit
import Network
import Data.Time.Clock
import Data.Time.Calendar
import qualified Control.Exception as E
past :: UTCTime
past = UTCTime (ModifiedJulianDay 56200) (secondsToDiffTime 0)
future :: UTCTime
future = UTCTime (ModifiedJulianDay 562000) (secondsToDiffTime 0)
cookie :: Cookie
cookie = Cookie { cookie_name = "password_hash"
               , cookie_value = "abf472c35f8297fbcabf2911230001234fd2"
               , cookie_expiry_time = future
               , cookie_domain = "example.com"
               , cookie_path = "/"
               , cookie_creation_time = past
               , cookie_last_access_time = past
               , cookie_persistent = False
               , cookie_host_only = False
               , cookie_secure_only = False
               , cookie_http_only = False
               }
main = withSocketsDo $ do
    request' <- parseUrl "http://example.com/secret-page"
    let request = request' { cookieJar = Just $ createCookieJar [cookie] }
    E.catch (withManager $ httpLbs request)
            ((StatusCodeException statusCode _ _) ->
              if statusCode==403 then putStrLn "login failed" else return ())

参考:http://hackage.haskell.org/package/http-conduit-2.0.0.0.4/docs/network-http-conduit.html

我加载它时的错误消息

samplecode.hs:33:39:
    Couldn't match type `()'
                  with `Response Data.ByteString.Lazy.Internal.ByteString'
    Expected type: IO
                     (Response Data.ByteString.Lazy.Internal.ByteString)
      Actual type: IO ()
    In the return type of a call of `putStrLn'
    In the expression: putStrLn "login failed"
    In the expression:
  if statusCode == 403 then putStrLn "login failed" else return ()
samplecode.hs:33:75:
    Couldn't match expected type `Response
                                    Data.ByteString.Lazy.Internal.ByteString'
                with actual type `()'
    In the first argument of `return', namely `()'
    In the expression: return ()
    In the expression:
      if statusCode == 403 then putStrLn "login failed" else return ()
Failed, modules loaded: none.

我该如何修复?

非常感谢

update

遵循亚伯拉罕森的建议,我将代码有所更改为以下内容,现在具有适当的状态coptexception处理。

main = withSocketsDo $ do
    request' <- parseUrl "http://example.com/secret-page"
    let request = request' { cookieJar = Just $ createCookieJar [cookie] }
    eitherResp <- E.try (withManager $ httpLbs request)
    case eitherResp of
      Left (StatusCodeException s _ _) 
        | statusCode s == 403 -> putStrLn "login failed"
        | otherwise         -> return () 
      Right resp -> print (L.length (responseBody resp))

您没有按预期使用E.catch。如果您看一下类型:

E.catch :: Exception e => IO a -> (e -> IO a) -> IO a

很明显,第一和第二参数的返回类型必须匹配。在您的情况下,您有

withManager $ httpLbs request :: IO (Response ByteString)

在第一个分支中,要么

putStrLn "login failed" -- or
return ()

在第二个。这些类型不匹配,因此您会收到您看到的错误。


在更高级别上,问题在于您没有处理成功案例。例如,我们可以使用E.try重写此功能,以使其更加清晰

eitherResp <- E.try (withManager $ httpLbs request)
case eitherResp of
  Left (StatusCodeException statusCode _ _)
    | statusCode == 403 -> putStrLn "login failed"
    | otherwise         -> return ()
  Right resp -> print (ByteString.length (responseBody resp))

在这里,由于我在Either StatusCodeException (Response ByteString)上明确匹配了模式,因此很明显,我需要同时提供失败的分支和后续的分支,并给他们相同的返回类型。为此,我引入了对成功案例的措施。

通常,我发现E.try易于使用。当您要在失败下提供默认值时,E.catch主要有用。

相关内容

  • 没有找到相关文章

最新更新