From 24bca7650e1f3316726f6e90a4616b87468cce3b Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 2 Jan 2021 16:55:43 +0000 Subject: [PATCH] throw exception from Transport.StartTCPClient when no provided address works (and on empty list of addresses) --- src/Simplex/Messaging/Transport.hs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 4e462663a..5f7ffde0f 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -11,13 +11,12 @@ import Control.Monad.IO.Unlift import Control.Monad.Reader import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B -import Data.Either import GHC.IO.Exception (IOErrorType (..)) import Network.Socket import System.IO import System.IO.Error import UnliftIO.Concurrent -import UnliftIO.Exception (SomeException (..)) +import UnliftIO.Exception (Exception, IOException) import qualified UnliftIO.Exception as E import qualified UnliftIO.IO as IO @@ -44,24 +43,22 @@ runTCPServer port server = acceptTCPConn :: MonadIO m => Socket -> m Handle acceptTCPConn sock = liftIO $ do (conn, _) <- accept sock - -- putStrLn $ "Accepted connection from " ++ show peer getSocketHandle conn startTCPClient :: MonadUnliftIO m => HostName -> ServiceName -> m Handle startTCPClient host port = liftIO . withSocketsDo $ - fromRight (error "can't connect") - <$> (resolve >>= foldM tryOpen (Left err)) + resolve >>= foldM tryOpen (Left err) >>= either E.throwIO return where - err :: SomeException - err = SomeException $ mkIOError NoSuchThing "no address tried" Nothing Nothing + err :: IOException + err = mkIOError NoSuchThing "no address" Nothing Nothing resolve :: IO [AddrInfo] resolve = do let hints = defaultHints {addrSocketType = Stream} getAddrInfo (Just hints) (Just host) (Just port) - tryOpen :: Either SomeException Handle -> AddrInfo -> IO (Either SomeException Handle) + tryOpen :: Exception e => Either e Handle -> AddrInfo -> IO (Either e Handle) tryOpen h@(Right _) _ = return h tryOpen (Left _) addr = E.try $ open addr