on master help sign in
Add isStatusCodeException
0928fc2Toshio Ito
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+
1## Req 3.11.0
6## Req 3.11.0
2
7
3* Add the `queryParamToList` method to the `QueryParam` type class.
8* Add the `queryParamToList` method to the `QueryParam` type class.
Network/HTTP/Req.hs+25
@@ -207,6 +207,7 @@ module Network.HTTP.Req
207
207
208 -- * Other
208 -- * Other
209 HttpException (..),
209 HttpException (..),
210+ isStatusCodeException,
210 CanHaveBody (..),
211 CanHaveBody (..),
211 Scheme (..),
212 Scheme (..),
212 )
213 )
@@ -1971,6 +1972,16 @@ class HttpResponse response where
1971 acceptHeader :: Proxy response -> Maybe ByteString
1972 acceptHeader :: Proxy response -> Maybe ByteString
1972 acceptHeader Proxy = Nothing
1973 acceptHeader Proxy = Nothing
1973
1974
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+
1974----------------------------------------------------------------------------
1985----------------------------------------------------------------------------
1975-- Other
1986-- Other
1976
1987
@@ -2009,6 +2020,20 @@ data HttpException
2009
2020
2010instance Exception HttpException
2021instance Exception HttpException
2011
2022
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+
2012-- | A simple type isomorphic to 'Bool' that we only have for better error
2037-- | A simple type isomorphic to 'Bool' that we only have for better error
2013-- messages. We use it as a kind and its data constructors as type-level
2038-- messages. We use it as a kind and its data constructors as type-level
2014-- tags.
2039-- tags.
httpbin-tests/Network/HTTP/ReqSpec.hs+12
@@ -49,6 +49,11 @@ spec = do
49 blindlyThrowing (req GET httpbin NoReqBody ignoreResponse mempty)
49 blindlyThrowing (req GET httpbin NoReqBody ignoreResponse mempty)
50 `shouldThrow` anyException
50 `shouldThrow` anyException
51
51
52+ describe "isStatusCodeException" $
53+ it "extracts non-2xx response" $
54+ req GET (httpbin /: "foo") NoReqBody ignoreResponse mempty
55+ `shouldThrow` selector404ByStatusCodeException
56+
52 describe "receiving user-agent header back" $
57 describe "receiving user-agent header back" $
53 it "works" $ do
58 it "works" $ do
54 r <-
59 r <-
@@ -454,6 +459,13 @@ selector404
454 L.responseStatus response == Y.status404 && not (B.null chunk)
459 L.responseStatus response == Y.status404 && not (B.null chunk)
455selector404 _ = False
460selector404 _ = False
456
461
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+
457-- | The empty JSON 'Object'.
469-- | The empty JSON 'Object'.
458emptyObject :: Value
470emptyObject :: Value
459emptyObject = Object Aeson.KeyMap.empty
471emptyObject = Object Aeson.KeyMap.empty