@@ -149,6 +149,7 @@ where
149149import Control.Applicative ((<|>))
150150import Control.Arrow (first, second)
151151import Control.Exception (try)
152+import Control.Monad
152153import Control.Monad.IO.Class
153154import Data.Aeson
154155import Data.ByteString (ByteString)
@@ -275,30 +276,44 @@ class MonadIO m => MonadHttp m where
275276-- making HTTP requests.
276277
277278data HttpConfig = HttpConfig
278− { httpConfigProxy :: !(Maybe L.Proxy)
279+ { httpConfigProxy :: !(Maybe L.Proxy)
279280 -- ^ Proxy to use. By default values of @HTTP_PROXY@ and @HTTPS_PROXY@
280281 -- environment variables are respected, this setting overwrites them.
281282 -- Default value: 'Nothing'.
282283 , httpConfigRedirectCount :: !Word
283284 -- ^ How many redirects to follow when getting a resource. Default
284285 -- value: 10.
285− , httpConfigAltManager :: !(Maybe L.Manager)
286+ , httpConfigAltManager :: !(Maybe L.Manager)
286287 -- ^ Alternative 'L.Manager' to use. 'Nothing' (default value) means
287288 -- that default implicit manager will be used (that's what you want in
288289 -- 99% of cases).
290+ , httpConfigCheckResponse :: L.Request -> L.Response L.BodyReader -> IO ()
291+ -- ^ Check the response immediately after receiving the status and
292+ -- headers. This is used for throwing exceptions on non-success status
293+ -- codes by default. Throwing is better then just returning a request
294+ -- with non-2xx status code because in that case something is wrong and
295+ -- we need a way to short-cut execution. The thrown exception is caught
296+ -- by the library though and is available in 'handleHttpException',
297+ -- which see.
289298 } deriving Typeable
290299
291300instance Default HttpConfig where
292301 def = HttpConfig
293302 { httpConfigProxy = Nothing
294303 , httpConfigRedirectCount = 10
295− , httpConfigAltManager = Nothing }
304+ , httpConfigAltManager = Nothing
305+ , httpConfigCheckResponse = \_ response ->
306+ let Y.Status statusCode _ = L.responseStatus response in
307+ unless (200 <= statusCode && statusCode < 300) $ do
308+ chunk <- BL.toStrict <$> L.brReadSome (L.responseBody response) 1024
309+ LI.throwHttp (L.StatusCodeException (() <$ response) chunk) }
296310
297311instance RequestComponent HttpConfig where
298312 getRequestMod HttpConfig {..} = Endo $ \x ->
299313 x { L.proxy = httpConfigProxy
300314 , L.redirectCount = fromIntegral httpConfigRedirectCount
301− , LI.requestManagerOverride = httpConfigAltManager }
315+ , LI.requestManagerOverride = httpConfigAltManager
316+ , LI.checkResponse = httpConfigCheckResponse }
302317
303318----------------------------------------------------------------------------
304319-- Request — Methods