From 4a30625a705dc6c18e555ab141393c7b4559a5e3 Mon Sep 17 00:00:00 2001 From: intractable Date: Mon, 23 Oct 2017 10:11:13 -0500 Subject: [PATCH] Expose `ClientError` type (#36) * Expose the max receive message length channel argument + unittest * `ClientError` GADT ctor => `ClientErrorResponse`, expose `ClientError` Prior to this commit, the name `ClientError` was both: - The name of a data constructor name for the `ClientResult` GADT - A name of an internal sum type for capturing different kinds of client errors We want to expose the latter to users of the library, and so expose the latter and rename the former. * Remove unused `MultiWayIf` lang ext --- examples/echo/echo-hs/EchoClient.hs | 2 +- src/Network/GRPC/HighLevel/Client.hs | 23 +++++++++++------------ src/Network/GRPC/HighLevel/Generated.hs | 1 + tests/TestClient.hs | 9 ++++----- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/examples/echo/echo-hs/EchoClient.hs b/examples/echo/echo-hs/EchoClient.hs index 268f86b..a84dd34 100644 --- a/examples/echo/echo-hs/EchoClient.hs +++ b/examples/echo/echo-hs/EchoClient.hs @@ -44,5 +44,5 @@ main = do | rsp == expected -> return () | otherwise -> fail $ "Got unexpected response: '" ++ show rsp ++ "', expected: '" ++ show expected ++ "'" ClientNormalResponse _ _ _ st _ -> fail $ "Got unexpected status " ++ show st ++ " from call, expecting StatusOk" - ClientError e -> fail $ "Got client error: " ++ show e + ClientErrorResponse e -> fail $ "Got client error: " ++ show e putStrLn $ "echo-client success: sent " ++ show pay ++ ", got " ++ show pay diff --git a/src/Network/GRPC/HighLevel/Client.hs b/src/Network/GRPC/HighLevel/Client.hs index 3d31b2d..8cdcf23 100644 --- a/src/Network/GRPC/HighLevel/Client.hs +++ b/src/Network/GRPC/HighLevel/Client.hs @@ -13,15 +13,15 @@ module Network.GRPC.HighLevel.Client , StreamSend , WritesDone , LL.Client - , ServiceClient + , ClientError(..) , ClientRequest(..) , ClientResult(..) --- , ClientResponse, response, initMD, trailMD, rspCode, details - , ClientRegisterable(..) + , clientRequest + ) - , clientRequest ) where +where import qualified Network.GRPC.LowLevel.Client as LL import qualified Network.GRPC.LowLevel.Call as LL @@ -62,8 +62,7 @@ data ClientResult (streamType :: GRPCMethodType) response where ClientWriterResponse :: Maybe response -> MetadataMap -> MetadataMap -> StatusCode -> StatusDetails -> ClientResult 'ClientStreaming response ClientReaderResponse :: MetadataMap -> StatusCode -> StatusDetails -> ClientResult 'ServerStreaming response ClientBiDiResponse :: MetadataMap -> StatusCode -> StatusDetails -> ClientResult 'BiDiStreaming response - - ClientError :: ClientError -> ClientResult streamType response + ClientErrorResponse :: ClientError -> ClientResult streamType response class ClientRegisterable (methodType :: GRPCMethodType) where clientRegisterMethod :: LL.Client @@ -92,30 +91,30 @@ clientRequest :: (Message request, Message response) => clientRequest client (RegisteredMethod method) (ClientNormalRequest req timeout meta) = mkResponse <$> LL.clientRequest client method timeout (BL.toStrict (toLazyByteString req)) meta where - mkResponse (Left ioError_) = ClientError (ClientIOError ioError_) + mkResponse (Left ioError_) = ClientErrorResponse (ClientIOError ioError_) mkResponse (Right rsp) = case fromByteString (LL.rspBody rsp) of - Left err -> ClientError (ClientErrorNoParse err) + Left err -> ClientErrorResponse (ClientErrorNoParse err) Right parsedRsp -> ClientNormalResponse parsedRsp (LL.initMD rsp) (LL.trailMD rsp) (LL.rspCode rsp) (LL.details rsp) clientRequest client (RegisteredMethod method) (ClientWriterRequest timeout meta handler) = mkResponse <$> LL.clientWriter client method timeout meta (handler . convertSend) where - mkResponse (Left ioError_) = ClientError (ClientIOError ioError_) + mkResponse (Left ioError_) = ClientErrorResponse (ClientIOError ioError_) mkResponse (Right (rsp_, initMD_, trailMD_, rspCode_, details_)) = case maybe (Right Nothing) (fmap Just . fromByteString) rsp_ of - Left err -> ClientError (ClientErrorNoParse err) + Left err -> ClientErrorResponse (ClientErrorNoParse err) Right parsedRsp -> ClientWriterResponse parsedRsp initMD_ trailMD_ rspCode_ details_ clientRequest client (RegisteredMethod method) (ClientReaderRequest req timeout meta handler) = mkResponse <$> LL.clientReader client method timeout (BL.toStrict (toLazyByteString req)) meta (\m recv -> handler m (convertRecv recv)) where - mkResponse (Left ioError_) = ClientError (ClientIOError ioError_) + mkResponse (Left ioError_) = ClientErrorResponse (ClientIOError ioError_) mkResponse (Right (meta_, rspCode_, details_)) = ClientReaderResponse meta_ rspCode_ details_ clientRequest client (RegisteredMethod method) (ClientBiDiRequest timeout meta handler) = mkResponse <$> LL.clientRW client method timeout meta (\_m recv send writesDone -> handler meta (convertRecv recv) (convertSend send) writesDone) where - mkResponse (Left ioError_) = ClientError (ClientIOError ioError_) + mkResponse (Left ioError_) = ClientErrorResponse (ClientIOError ioError_) mkResponse (Right (meta_, rspCode_, details_)) = ClientBiDiResponse meta_ rspCode_ details_ diff --git a/src/Network/GRPC/HighLevel/Generated.hs b/src/Network/GRPC/HighLevel/Generated.hs index caac181..cc55234 100644 --- a/src/Network/GRPC/HighLevel/Generated.hs +++ b/src/Network/GRPC/HighLevel/Generated.hs @@ -32,6 +32,7 @@ module Network.GRPC.HighLevel.Generated ( -- * Client , withGRPCClient , ClientConfig(..) +, ClientError(..) , ClientRequest(..) , ClientResult(..) ) diff --git a/tests/TestClient.hs b/tests/TestClient.hs index dfa7599..4c4f094 100644 --- a/tests/TestClient.hs +++ b/tests/TestClient.hs @@ -39,7 +39,7 @@ testNormalCall client = testCase "Normal call" $ res <- simpleServiceNormalCall client (ClientNormalRequest req 10 mempty) case res of - ClientError err -> assertString ("ClientError: " <> show err) + ClientErrorResponse err -> assertString ("ClientErrorResponse: " <> show err) ClientNormalResponse res _ _ stsCode _ -> do stsCode @?= StatusOk simpleServiceResponseResponse res @?= "NormalRequest" @@ -60,7 +60,7 @@ testClientStreamingCall client = testCase "Client-streaming call" $ (finalName, totalSum) <- readMVar v case res of - ClientError err -> assertString ("ClientError: " <> show err) + ClientErrorResponse err -> assertString ("ClientErrorResponse: " <> show err) ClientWriterResponse Nothing _ _ _ _ -> assertString "No response received" ClientWriterResponse (Just res) _ _ stsCode _ -> do stsCode @?= StatusOk @@ -90,7 +90,7 @@ testServerStreamingCall client = testCase "Server-streaming call" $ ClientReaderRequest (SimpleServiceRequest "Test" (fromList nums)) 10 mempty (\_ -> checkResults nums) case res of - ClientError err -> assertString ("ClientError: " <> show err) + ClientErrorResponse err -> assertString ("ClientErrorResponse: " <> show err) ClientReaderResponse _ sts _ -> sts @?= StatusOk @@ -116,7 +116,7 @@ testBiDiStreamingCall client = testCase "Bidi-streaming call" $ res <- simpleServiceBiDiStreamingCall client $ ClientBiDiRequest 10 mempty (\_ -> handleRequests iterations) case res of - ClientError err -> assertString ("ClientError: " <> show err) + ClientErrorResponse err -> assertString ("ClientErrorResponse: " <> show err) ClientBiDiResponse _ sts _ -> sts @?= StatusOk @@ -133,4 +133,3 @@ main = do , testServerStreamingCall service , testBiDiStreamingCall service ]) `finally` (simpleServiceDone service (ClientNormalRequest SimpleServiceDone 10 mempty)) -