on master help sign in
catch and report exceptions thrown from connection creation

resolves #275

commit97c38dff561c199a02b23b377f910d14b916d966 authorAgentM <agentm@themactionfaction.com> dateSun 17 Jan 2021, 19:15 (6 years ago) parent4cb9080
2 files changed+22−10
src/lib/ProjectM36/Client.hs+11−9 test/Server/Main.hs+11−1
src/lib/ProjectM36/Client.hs+11−9
@@ -297,16 +297,18 @@ connectProjectM36 (RemoteConnectionInfo dbName hostName servicePort notification
297297 \(NotificationMessage notifications') ->
298298 forM_ (M.toList notifications') (uncurry notificationCallback)
299299 ]
300−-- TODO missing async callback
301− conn <- RPC.connect notificationHandlers (hostAddressToTuple addr) port
302− eRet <- RPC.call conn (Login dbName)
303−-- TODO handle connection errors
304− case eRet of
305− Left err -> error (show err)
306− Right False -> error "wtf"
307− Right True ->
300+ let connectExcHandler (e :: IOException) = pure $ Left (IOExceptionError e)
301+ eConn <- (Right <$> RPC.connect notificationHandlers (hostAddressToTuple addr) port) `catch` connectExcHandler
302+ case eConn of
303+ Left err -> pure (Left err)
304+ Right conn -> do
305+ eRet <- RPC.call conn (Login dbName)
306+ case eRet of
307+ Left err -> error (show err)
308+ Right False -> error "wtf"
309+ Right True ->
308310 --TODO handle connection errors!
309− pure (Right (RemoteConnection (RemoteConnectionConf conn)))
311+ pure (Right (RemoteConnection (RemoteConnectionConf conn)))
310312
311313--convert RPC errors into exceptions
312314convertRPCErrors :: RPC.ConnectionError -> IO a
test/Server/Main.hs+11−1
@@ -44,7 +44,7 @@ testList sessionId conn notificationTestMVar = TestList $ serverTests ++ session
4444 testRelationVariableSummary,
4545 testNotification notificationTestMVar
4646 ]
47− serverTests = [testRequestTimeout, testFileDescriptorCount]
47+ serverTests = [testRequestTimeout, testFileDescriptorCount, testClientConnectFail]
4848
4949main :: IO ()
5050main = do
@@ -240,3 +240,13 @@ fdCount = do
240240--pass on non-linux platforms
241241testFileDescriptorCount = TestCase (pure ())
242242#endif
243+
244+testClientConnectFail :: Test
245+testClientConnectFail = TestCase $ do
246+ let connInfo = RemoteConnectionInfo "nonexistentdb" "127.0.0.1" "7777" emptyNotificationCallback
247+ eConn <- connectProjectM36 connInfo
248+ case eConn of
249+ Left (IOExceptionError _) -> pure ()
250+ _ -> assertFailure "connection failure failed"
251+
252+