Skip to content

Commit

Permalink
Extract executeOrPanic
Browse files Browse the repository at this point in the history
  • Loading branch information
diogob committed May 2, 2024
1 parent 088cb6f commit 4901795
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Hasql/Notifications.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ listen ::
listen con channel =
void $ withLibPQConnection con execListen
where
execListen pqCon = do
result <- PQ.exec pqCon $ T.encodeUtf8 $ "LISTEN " <> fromPgIdentifier channel
when (isNothing result) $ do
mError <- PQ.errorMessage pqCon
panic $ maybe "Error executing LISTEN" (T.unpack . T.decodeUtf8Lenient) mError
execListen = executeOrPanic $ T.encodeUtf8 $ "LISTEN " <> fromPgIdentifier channel

-- | Given a Hasql Connection and a channel sends a unlisten command to the database
unlisten ::
Expand All @@ -134,11 +130,14 @@ unlisten ::
unlisten con channel =
void $ withLibPQConnection con execUnlisten
where
execUnlisten pqCon = do
result <- PQ.exec pqCon $ T.encodeUtf8 $ "UNLISTEN " <> fromPgIdentifier channel
when (isNothing result) $ do
mError <- PQ.errorMessage pqCon
panic $ maybe "Error executing UNLISTEN" (T.unpack . T.decodeUtf8Lenient) mError
execUnlisten = executeOrPanic $ T.encodeUtf8 $ "UNLISTEN " <> fromPgIdentifier channel

executeOrPanic :: ByteString -> PQ.Connection -> IO ()
executeOrPanic cmd pqCon = do
result <- PQ.exec pqCon cmd
when (isNothing result) $ do
mError <- PQ.errorMessage pqCon
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

0 comments on commit 4901795

Please sign in to comment.