Skip to content

Commit

Permalink
Including response body for failed status checks
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
brendanhay committed May 11, 2016
1 parent a1a5982 commit 7240eb1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions gogol/gogol.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ library
, base >= 4.7
, bytestring >= 0.9
, case-insensitive >= 1.2
, conduit >= 1.1
, conduit-extra >= 1.1
, cryptonite >= 0.6
, data-default-class >= 0.0.1
Expand Down
25 changes: 16 additions & 9 deletions gogol/src/Network/Google/Internal/HTTP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import Control.Lens ((%~), (&))
import Control.Monad.Catch
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Trans.Resource (MonadResource (..))
import qualified Data.ByteString.Lazy as LBS
import Data.Conduit (($$+-))
import qualified Data.Conduit.List as Conduit
import Data.Default.Class (Default (..))
import Data.Monoid (Dual (..), Endo (..), (<>))
import qualified Data.Text.Encoding as Text
Expand Down Expand Up @@ -64,6 +67,8 @@ perform Env{..} x = catches go handlers

logDebug _envLogger rs -- debug:ClientResponse

statusCheck rs

r <- _cliResponse (responseBody rs)

pure $! case r of
Expand All @@ -80,7 +85,7 @@ perform Env{..} x = catches go handlers
{ Client.host = _svcHost
, Client.port = _svcPort
, Client.secure = _svcSecure
, Client.checkStatus = status
, Client.checkStatus = \_ _ _ -> Nothing
, Client.responseTimeout = timeout
, Client.method = _cliMethod
, Client.path = path
Expand All @@ -97,14 +102,16 @@ perform Env{..} x = catches go handlers
. LText.toStrict
$ Build.toLazyText (_svcPath <> _rqPath)

status s hs _
| _cliCheck s = Nothing
| otherwise = Just . toException . ServiceError $ ServiceError'
{ _serviceId = _svcId
, _serviceStatus = s
, _serviceHeaders = hs
, _serviceBody = Nothing
}
statusCheck rs
| _cliCheck (responseStatus rs) = pure ()
| otherwise = do
b <- LBS.fromChunks <$> (responseBody rs $$+- Conduit.consume)
throwM . toException . ServiceError $ ServiceError'
{ _serviceId = _svcId
, _serviceStatus = responseStatus rs
, _serviceHeaders = responseHeaders rs
, _serviceBody = Just b
}

timeout = microseconds <$> _svcTimeout

Expand Down

0 comments on commit 7240eb1

Please sign in to comment.