git.iko.soy/typeable/postgresql-simple
on master help sign in
Throw a better exception from getNotification
4cfcdbeLeon P Smith
src/Database/PostgreSQL/Simple/Notification.hs+3−4
@@ -36,6 +36,7 @@ module Database.PostgreSQL.Simple.Notification
36
36
37import Control.Concurrent
37import Control.Concurrent
38import Control.Monad ( when )
38import Control.Monad ( when )
39+import Control.Exception ( throwIO )
39import qualified Data.ByteString as B
40import qualified Data.ByteString as B
40import Database.PostgreSQL.Simple.Internal
41import Database.PostgreSQL.Simple.Internal
41import qualified Database.PostgreSQL.LibPQ as PQ
42import qualified Database.PostgreSQL.LibPQ as PQ
@@ -47,9 +48,6 @@ data Notification = Notification
47 , notificationData :: !B.ByteString
48 , notificationData :: !B.ByteString
48 }
49 }
49
50
50−errfd :: String
51−errfd = "Database.PostgreSQL.Simple.Notification.getNotification: failed to fetch file descriptor"
52−
53convertNotice :: PQ.Notify -> Notification
51convertNotice :: PQ.Notify -> Notification
54convertNotice PQ.Notify{..}
52convertNotice PQ.Notify{..}
55 = Notification { notificationPid = notifyBePid
53 = Notification { notificationPid = notifyBePid
@@ -62,6 +60,7 @@ convertNotice PQ.Notify{..}
62getNotification :: Connection -> IO Notification
60getNotification :: Connection -> IO Notification
63getNotification conn = loop False
61getNotification conn = loop False
64 where
62 where
63+ funcName = "Database.PostgreSQL.Simple.Notification.getNotification"
65 loop doConsume = do
64 loop doConsume = do
66 res <- withConnection conn $ \c -> do
65 res <- withConnection conn $ \c -> do
67 when doConsume (PQ.consumeInput c >> return ())
66 when doConsume (PQ.consumeInput c >> return ())
@@ -70,7 +69,7 @@ getNotification conn = loop False
70 Nothing -> do
69 Nothing -> do
71 mfd <- PQ.socket c
70 mfd <- PQ.socket c
72 case mfd of
71 case mfd of
73− Nothing -> fail errfd
72+ Nothing -> throwIO $ fdError funcName
74 Just fd -> return (Left fd)
73 Just fd -> return (Left fd)
75 Just msg -> return (Right msg)
74 Just msg -> return (Right msg)
76 -- FIXME? what happens if the connection is closed/reset right here?
75 -- FIXME? what happens if the connection is closed/reset right here?