git.iko.soy/typeable/servant
on master help sign in
Merge pull request #94 from haskell-servant/tvh/simpler-error
fa6cfbbAlp Mestanogullari
servant-client/src/Servant/Common/Req.hs+10−9
@@ -48,13 +48,13 @@ data ServantError
48 { responseContentType :: MediaType
48 { responseContentType :: MediaType
49 , responseBody :: ByteString
49 , responseBody :: ByteString
50 }
50 }
51− | ConnectionError
52− { connectionError :: HttpException
53− }
54 | InvalidContentTypeHeader
51 | InvalidContentTypeHeader
55 { responseContentTypeHeader :: ByteString
52 { responseContentTypeHeader :: ByteString
56 , responseBody :: ByteString
53 , responseBody :: ByteString
57 }
54 }
55+ | ConnectionError
56+ { connectionError :: SomeException
57+ }
58 deriving (Show, Typeable)
58 deriving (Show, Typeable)
59
59
60instance Exception ServantError
60instance Exception ServantError
@@ -152,11 +152,11 @@ performRequest reqMethod req isWantedStatus reqHost = do
152 }
152 }
153
153
154 eResponse <- liftIO $ __withGlobalManager $ \ manager ->
154 eResponse <- liftIO $ __withGlobalManager $ \ manager ->
155− catchHttpException $
155+ catchConnectionError $
156− Client.httpLbs request manager
156+ Client.httpLbs request manager
157 case eResponse of
157 case eResponse of
158 Left err ->
158 Left err ->
159− left $ ConnectionError err
159+ left . ConnectionError $ SomeException err
160
160
161 Right response -> do
161 Right response -> do
162 let status = Client.responseStatus response
162 let status = Client.responseStatus response
@@ -189,6 +189,7 @@ performRequestNoBody reqMethod req wantedStatus reqHost = do
189 _ <- performRequest reqMethod req (`elem` wantedStatus) reqHost
189 _ <- performRequest reqMethod req (`elem` wantedStatus) reqHost
190 return ()
190 return ()
191
191
192−catchHttpException :: IO a -> IO (Either HttpException a)
192+catchConnectionError :: IO a -> IO (Either ServantError a)
193−catchHttpException action =
193+catchConnectionError action =
194− catch (Right <$> action) (pure . Left)
194+ catch (Right <$> action) $ \e ->
195+ pure . Left . ConnectionError $ SomeException (e :: HttpException)
servant-client/test/Servant/ClientSpec.hs+1−6
@@ -68,11 +68,6 @@ instance FromFormUrlEncoded Person where
68 a <- lookupEither "age" xs
68 a <- lookupEither "age" xs
69 return $ Person (T.unpack n) (read $ T.unpack a)
69 return $ Person (T.unpack n) (read $ T.unpack a)
70
70
71−deriving instance Eq ServantError
72−
73−instance Eq C.HttpException where
74− a == b = show a == show b
75−
76alice :: Person
71alice :: Person
77alice = Person "Alice" 42
72alice = Person "Alice" 42
78
73
@@ -311,7 +306,7 @@ failSpec = withFailServer $ \ baseUrl -> do
311 it "reports ConnectionError" $ do
306 it "reports ConnectionError" $ do
312 Left res <- runEitherT getGetWrongHost
307 Left res <- runEitherT getGetWrongHost
313 case res of
308 case res of
314− ConnectionError (C.FailedConnectionException2 "127.0.0.1" 19872 False _) -> return ()
309+ ConnectionError _ -> return ()
315 _ -> fail $ "expected ConnectionError, but got " <> show res
310 _ -> fail $ "expected ConnectionError, but got " <> show res
316
311
317 it "reports UnsupportedContentType" $ do
312 it "reports UnsupportedContentType" $ do