git.iko.soy/typeable/postgresql-simple
on master help sign in
Make a version of withTransactionModeRetry that lets you pick exception type
commit9b244819156ac3ec68707db54f75c70dcc6ee142 authorElliot Cameron <eacameron@gmail.com> dateSat 9 May 2020, 11:26 (6 years ago) parent10d2913
1 file changed+10−6
src/Database/PostgreSQL/Simple/Transaction.hs+10−6
src/Database/PostgreSQL/Simple/Transaction.hs+10−6
@@ -146,22 +146,26 @@ withTransactionMode mode conn act =
146146 commit conn
147147 return r
148148
149+-- | 'withTransactionModeRetry'' but with the exception type pinned to 'SqlError'.
150+withTransactionModeRetry :: forall a. TransactionMode -> (SqlError -> Bool) -> Connection -> IO a -> IO a
151+withTransactionModeRetry = withTransactionModeRetry'
152+
149153-- | Like 'withTransactionMode', but also takes a custom callback to
150−-- determine if a transaction should be retried if an 'SqlError' occurs.
151−-- If the callback returns True, then the transaction will be retried.
152−-- If the callback returns False, or an exception other than an 'SqlError'
154+-- determine if a transaction should be retried if an exception occurs.
155+-- If the callback returns 'True', then the transaction will be retried.
156+-- If the callback returns 'False', or an exception other than an @e@
153157-- occurs then the transaction will be rolled back and the exception rethrown.
154158--
155159-- This is used to implement 'withTransactionSerializable'.
156−withTransactionModeRetry :: TransactionMode -> (SqlError -> Bool) -> Connection -> IO a -> IO a
157−withTransactionModeRetry mode shouldRetry conn act =
160+withTransactionModeRetry' :: forall a e. E.Exception e => TransactionMode -> (e -> Bool) -> Connection -> IO a -> IO a
161+withTransactionModeRetry' mode shouldRetry conn act =
158162 mask $ \restore ->
159163 retryLoop $ E.try $ do
160164 a <- restore act `E.onException` rollback_ conn
161165 commit conn
162166 return a
163167 where
164− retryLoop :: IO (Either SqlError a) -> IO a
168+ retryLoop :: IO (Either e a) -> IO a
165169 retryLoop act' = do
166170 beginMode mode conn
167171 r <- act'