Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Response Body in Raised Errors #23

Merged
merged 1 commit into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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