Browse files

Include first 1024 bytes of response in StatusCodeException

  • Loading branch information...
1 parent 5240ab5 commit 5bbe2bc02d5ba3390783d081298284d91fbbf803 @snoyberg committed Sep 30, 2012
Showing with 14 additions and 4 deletions.
  1. +13 −3 Network/HTTP/Conduit.hs
  2. +1 −1 http-conduit.cabal
View
16 Network/HTTP/Conduit.hs
@@ -147,7 +147,9 @@ import Control.Monad ((<=<))
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Trans.Control (MonadBaseControl)
+import Control.Exception (fromException, toException)
import qualified Data.Conduit as C
+import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.Internal as CI
import Data.Conduit.Blaze (builderToByteString)
import Data.Conduit (MonadResource)
@@ -193,9 +195,17 @@ http req0 manager = do
case checkStatus req0 status hs of
Nothing -> return res
Just exc -> do
- let CI.ResumableSource _ final = body
- final
- liftIO $ throwIO exc
+ exc' <-
+ case fromException exc of
+ Just (StatusCodeException s hs) -> do
+ lbs <- body C.$$+- CB.take 1024
+ return $ toException $ StatusCodeException s $ hs ++
+ [("X-Response-Body-Start", S.concat $ L.toChunks lbs)]
+ _ -> do
+ let CI.ResumableSource _ final = body
+ final
+ return exc
+ liftIO $ throwIO exc'
where
go (-1) _ _ ress = liftIO . throwIO . TooManyRedirects =<< mapM lbsResponse ress
go count req'' cookie_jar'' ress = do
View
2 http-conduit.cabal
@@ -1,5 +1,5 @@
name: http-conduit
-version: 1.6.1
+version: 1.6.1.1
license: BSD3
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>

4 comments on commit 5bbe2bc

@snoyberg
Owner
  1. It requires an API change.
  2. Grabbing the entire response body like that can be incredibly expensive in some cases.
  3. Even if debugging is turned off I'd like to get some idea of what went wrong. 1024 bytes of response body is usually enough to see what the web server sent.
@exbb2
  • 1. Whatever
  • 2/3. Wouldn't including ResumableSource in exceptions allow us to download whatever amount of data we feel fine with?
@snoyberg
Owner

Including a ResumableSource will likely lead to one of two situations:

  1. Leaking file descriptors
  2. Accidentally trying to pull from a ResumableSource after the underlying Socket has been closed.
Please sign in to comment.