Skip to content

Commit

Permalink
Merge pull request #25 from diogob/do-not-fail-silently
Browse files Browse the repository at this point in the history
Report errors when LISTEN or UNLISTEN fail
  • Loading branch information
diogob authored May 5, 2024
2 parents c8f4956 + f1758f7 commit 122a92c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/Hasql/Notifications.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where

import Control.Concurrent (threadDelay, threadWaitRead)
import Control.Exception (Exception, throw)
import Control.Monad (forever, unless, void)
import Control.Monad (forever, unless, void, when)
import Data.ByteString.Char8 (ByteString)
import Data.Functor.Contravariant (contramap)
import Data.Text (Text)
Expand Down Expand Up @@ -117,7 +117,7 @@ listen ::
listen con channel =
void $ withLibPQConnection con execListen
where
execListen pqCon = void $ PQ.exec pqCon $ T.encodeUtf8 $ "LISTEN " <> fromPgIdentifier channel
execListen = executeOrPanic $ T.encodeUtf8 $ "LISTEN " <> fromPgIdentifier channel

-- | Given a Hasql Connection and a channel sends a unlisten command to the database
unlisten ::
Expand All @@ -127,9 +127,22 @@ unlisten ::
PgIdentifier ->
IO ()
unlisten con channel =
void $ withLibPQConnection con execListen
void $ withLibPQConnection con execUnlisten
where
execListen pqCon = void $ PQ.exec pqCon $ T.encodeUtf8 $ "UNLISTEN " <> fromPgIdentifier channel
execUnlisten = executeOrPanic $ T.encodeUtf8 $ "UNLISTEN " <> fromPgIdentifier channel

executeOrPanic :: ByteString -> PQ.Connection -> IO ()
executeOrPanic cmd pqCon = do
mResult <- PQ.exec pqCon cmd
case mResult of
Nothing -> do
mError <- PQ.errorMessage pqCon
panic $ maybe ("Error executing" <> show cmd) (T.unpack . T.decodeUtf8Lenient) mError
Just result -> do
status <- PQ.resultStatus result
when (status == PQ.FatalError) $ do
mError <- PQ.resultErrorMessage result
panic $ maybe ("Error executing" <> show cmd) (T.unpack . T.decodeUtf8Lenient) mError

-- |
-- Given a function that handles notifications and a Hasql connection it will listen
Expand Down Expand Up @@ -186,5 +199,6 @@ waitForNotifications sendNotification con =
panic $ maybe "Error checking for PostgreSQL notifications" (T.unpack . T.decodeUtf8Lenient) mError
Just notification ->
sendNotification (PQ.notifyRelname notification) (PQ.notifyExtra notification)
panic :: String -> a
panic a = throw (FatalError a)

panic :: String -> a
panic a = throw (FatalError a)

0 comments on commit 122a92c

Please sign in to comment.