Haddock code doesn't compile

34 views
Skip to first unread message

Sibi Prabakaran

unread,
Aug 22, 2014, 6:40:19 PM8/22/14
to yeso...@googlegroups.com
This is one of the haddock code from http-conduit (I have changed slightly to include one import):

{-# LANGUAGE OverloadedStrings #-}
 import Network.HTTP.Conduit
 import Network
 import Data.Time.Clock
 import Data.Time.Calendar
 import qualified Control.Exception as E
 import Network.HTTP.Types.Status (statusCode)

 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 s _ _) ->
          if statusCode s==403 then putStrLn "login failed" else return ())

It produces the following compile error:

Expected type: IO (Response Data.ByteString.Lazy.Internal.ByteString)
Actual type: IO ()

On inspecting further, it becomes obvious that (withManager $ httpLbs request) has type of `IO (Response ByteString)
whereas (\(StatusCodeException s _ _) -> if statusCode s==403 then putStrLn "login failed" else return ()) has a type of
IO (). E.catch constrains them to have a single type. Any approaches to solve this problem ?

Regards,
Sibi

David McBride

unread,
Aug 22, 2014, 11:26:06 PM8/22/14
to yeso...@googlegroups.com
You'll have to deal with the fact that if this fails you need to return something you can use.  An easy way to do this is to (untested) replace withManager (httpLbs request) with fmap Just $ withManager (httpLbs request).  Then instead of returning (), you return Nothing.  You'll end up with a type of IO (Maybe (Response ByteString)) and you can match on that.

Another alternative is to process what you wanted from the response and return that, then return some alternative of the same type that is a suitable replacement when it is impossible to get an answer.


--
You received this message because you are subscribed to the Google Groups "Yesod Web Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yesodweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sibi Prabakaran

unread,
Aug 23, 2014, 6:03:21 PM8/23/14
to yeso...@googlegroups.com
Thanks, that sounds reasonable.

I have sent an appropriate pull request fixing the haddock code. Let me know if you have any further comments.

Regards,
Sibi

Michael Snoyman

unread,
Aug 24, 2014, 5:01:57 AM8/24/14
to yeso...@googlegroups.com
Thanks for looking into this guys, pull request merged.
Reply all
Reply to author
Forward
0 new messages