on dev help sign in
PQState
1a65af7Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+9−5
@@ -64,6 +64,7 @@ module Squeal.PostgreSQL.PQ
64 , resultErrorCode
64 , resultErrorCode
65 -- * Exceptions
65 -- * Exceptions
66 , SquealException (..)
66 , SquealException (..)
67+ , PQState (..)
67 , catchSqueal
68 , catchSqueal
68 , handleSqueal
69 , handleSqueal
69 ) where
70 ) where
@@ -613,14 +614,17 @@ resultErrorCode
613 -> io (Maybe ByteString)
614 -> io (Maybe ByteString)
614resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
615resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
615
616
616−-- | `Exception`s that can be thrown by Squeal.
617+-- | the state of LibPQ
617−data SquealException
618+data PQState = PQState
618− = PQException
619 { sqlExecStatus :: LibPQ.ExecStatus
619 { sqlExecStatus :: LibPQ.ExecStatus
620 , sqlStateCode :: Maybe ByteString
620 , sqlStateCode :: Maybe ByteString
621 -- ^ https://www.postgresql.org/docs/current/static/errcodes-appendix.html
621 -- ^ https://www.postgresql.org/docs/current/static/errcodes-appendix.html
622 , sqlErrorMessage :: Maybe ByteString
622 , sqlErrorMessage :: Maybe ByteString
623− }
623+ } deriving (Eq, Show)
624+
625+-- | `Exception`s that can be thrown by Squeal.
626+data SquealException
627+ = PQException PQState
624 | ResultException Text
628 | ResultException Text
625 | ParseException Text
629 | ParseException Text
626 deriving (Eq, Show)
630 deriving (Eq, Show)
@@ -638,7 +642,7 @@ tryResult result = liftIO $ do
638 _ -> do
642 _ -> do
639 stateCode <- LibPQ.resultErrorField result LibPQ.DiagSqlstate
643 stateCode <- LibPQ.resultErrorField result LibPQ.DiagSqlstate
640 msg <- LibPQ.resultErrorMessage result
644 msg <- LibPQ.resultErrorMessage result
641− throw $ PQException status stateCode msg
645+ throw . PQException $ PQState status stateCode msg
642
646
643-- | Catch `SquealException`s.
647-- | Catch `SquealException`s.
644catchSqueal
648catchSqueal
squeal-postgresql/src/Squeal/PostgreSQL/Transaction.hs+6−2
@@ -87,8 +87,12 @@ transactionallyRetry mode tx = mask $ \restore ->
87 loop attempt = do
87 loop attempt = do
88 begin mode
88 begin mode
89 attempt >>= \case
89 attempt >>= \case
90− Left (PQException _ (Just "40001") _) -> rollback >> loop attempt
90+ Left (PQException (PQState _ (Just "40001") _)) -> do
91+ rollback
92+ loop attempt
93+ Left err -> do
94+ rollback
91− Left err -> rollback >> throwIO err
95+ throwIO err
92 Right x -> return x
96 Right x -> return x
93
97
94-- | @BEGIN@ a transaction.
98-- | @BEGIN@ a transaction.