on master help sign in
Add isStatusCodeException
commit0928fc291507e60f9a533ad890477f5ed22fac5f authorToshio Ito <debug.ito@gmail.com> dateThu 21 Apr 2022, 14:57 (4 years ago) parent2ea79ae
3 files changed+42
CHANGELOG.md+5 Network/HTTP/Req.hs+25 httpbin-tests/Network/HTTP/ReqSpec.hs+12
CHANGELOG.md+5
@@ -1,3 +1,8 @@
1+## Req 3.12.0
2+
3+* Add `isStatusCodeException` function.
4+* Add `instance HttpResponse (Network.HTTP.Client.Response ())`.
5+
16## Req 3.11.0
27
38* Add the `queryParamToList` method to the `QueryParam` type class.
Network/HTTP/Req.hs+25
@@ -207,6 +207,7 @@ module Network.HTTP.Req
207207
208208 -- * Other
209209 HttpException (..),
210+ isStatusCodeException,
210211 CanHaveBody (..),
211212 Scheme (..),
212213 )
@@ -1971,6 +1972,16 @@ class HttpResponse response where
19711972 acceptHeader :: Proxy response -> Maybe ByteString
19721973 acceptHeader Proxy = Nothing
19731974
1975+-- | This instance has been added to make it easier to inspect 'L.Response'
1976+-- using Req's functions like 'responseStatusCode', 'responseStatusMessage',
1977+-- etc.
1978+--
1979+-- @since 3.12.0
1980+instance HttpResponse (L.Response ()) where
1981+ type HttpResponseBody (L.Response ()) = ()
1982+ toVanillaResponse = id
1983+ getHttpResponse = return . void
1984+
19741985----------------------------------------------------------------------------
19751986-- Other
19761987
@@ -2009,6 +2020,20 @@ data HttpException
20092020
20102021instance Exception HttpException
20112022
2023+-- | Return 'Just' if the given 'HttpException' is wrapping a http-client's
2024+-- 'L.StatusCodeException'. Otherwise, return 'Nothing'.
2025+--
2026+-- @since 3.12.0
2027+isStatusCodeException :: HttpException -> Maybe (L.Response ())
2028+isStatusCodeException
2029+ ( VanillaHttpException
2030+ ( L.HttpExceptionRequest
2031+ _
2032+ (L.StatusCodeException r _)
2033+ )
2034+ ) = Just r
2035+isStatusCodeException _ = Nothing
2036+
20122037-- | A simple type isomorphic to 'Bool' that we only have for better error
20132038-- messages. We use it as a kind and its data constructors as type-level
20142039-- tags.
httpbin-tests/Network/HTTP/ReqSpec.hs+12
@@ -49,6 +49,11 @@ spec = do
4949 blindlyThrowing (req GET httpbin NoReqBody ignoreResponse mempty)
5050 `shouldThrow` anyException
5151
52+ describe "isStatusCodeException" $
53+ it "extracts non-2xx response" $
54+ req GET (httpbin /: "foo") NoReqBody ignoreResponse mempty
55+ `shouldThrow` selector404ByStatusCodeException
56+
5257 describe "receiving user-agent header back" $
5358 it "works" $ do
5459 r <-
@@ -454,6 +459,13 @@ selector404
454459 L.responseStatus response == Y.status404 && not (B.null chunk)
455460selector404 _ = False
456461
462+-- | Same as 'selector404' except that it uses 'isStatusCodeException'.
463+selector404ByStatusCodeException :: HttpException -> Bool
464+selector404ByStatusCodeException e =
465+ case isStatusCodeException e of
466+ Nothing -> False
467+ Just r -> responseStatusCode r == 404
468+
457469-- | The empty JSON 'Object'.
458470emptyObject :: Value
459471emptyObject = Object Aeson.KeyMap.empty