on dev help sign in
PQState
commit1a65af78c97223fe548dfd23f23cf324afa44213 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateSat 13 Apr 2019, 21:03 (8 years ago) parentb333355
2 files changed+15−7
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+9−5 squeal-postgresql/src/Squeal/PostgreSQL/Transaction.hs+6−2
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+9−5
@@ -64,6 +64,7 @@ module Squeal.PostgreSQL.PQ
6464 , resultErrorCode
6565 -- * Exceptions
6666 , SquealException (..)
67+ , PQState (..)
6768 , catchSqueal
6869 , handleSqueal
6970 ) where
@@ -613,14 +614,17 @@ resultErrorCode
613614 -> io (Maybe ByteString)
614615resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
615616
616−-- | `Exception`s that can be thrown by Squeal.
617−data SquealException
618− = PQException
617+-- | the state of LibPQ
618+data PQState = PQState
619619 { sqlExecStatus :: LibPQ.ExecStatus
620620 , sqlStateCode :: Maybe ByteString
621621 -- ^ https://www.postgresql.org/docs/current/static/errcodes-appendix.html
622622 , sqlErrorMessage :: Maybe ByteString
623− }
623+ } deriving (Eq, Show)
624+
625+-- | `Exception`s that can be thrown by Squeal.
626+data SquealException
627+ = PQException PQState
624628 | ResultException Text
625629 | ParseException Text
626630 deriving (Eq, Show)
@@ -638,7 +642,7 @@ tryResult result = liftIO $ do
638642 _ -> do
639643 stateCode <- LibPQ.resultErrorField result LibPQ.DiagSqlstate
640644 msg <- LibPQ.resultErrorMessage result
641− throw $ PQException status stateCode msg
645+ throw . PQException $ PQState status stateCode msg
642646
643647-- | Catch `SquealException`s.
644648catchSqueal
squeal-postgresql/src/Squeal/PostgreSQL/Transaction.hs+6−2
@@ -87,8 +87,12 @@ transactionallyRetry mode tx = mask $ \restore ->
8787 loop attempt = do
8888 begin mode
8989 attempt >>= \case
90− Left (PQException _ (Just "40001") _) -> rollback >> loop attempt
91− Left err -> rollback >> throwIO err
90+ Left (PQException (PQState _ (Just "40001") _)) -> do
91+ rollback
92+ loop attempt
93+ Left err -> do
94+ rollback
95+ throwIO err
9296 Right x -> return x
9397
9498-- | @BEGIN@ a transaction.