diff --git a/postgrest.cabal b/postgrest.cabal index c4a289da4..21e51016e 100644 --- a/postgrest.cabal +++ b/postgrest.cabal @@ -40,6 +40,7 @@ executable postgrest , http-types , interpolatedstring-perl6 , jwt + , mtl , optparse-applicative >= 0.11 && < 0.13 , parsec , postgrest @@ -92,6 +93,7 @@ library , http-types , interpolatedstring-perl6 , jwt + , mtl , optparse-applicative , parsec , regex-tdfa @@ -173,6 +175,7 @@ Test-Suite spec , interpolatedstring-perl6 , jwt , monad-control + , mtl , optparse-applicative , parsec , process diff --git a/src/PostgREST/Main.hs b/src/PostgREST/Main.hs index 1200aea5e..c8931da2a 100644 --- a/src/PostgREST/Main.hs +++ b/src/PostgREST/Main.hs @@ -14,6 +14,7 @@ import PostgREST.Middleware import PostgREST.Types (DbStructure) import Control.Monad +import Control.Monad.Error.Class (catchError, throwError) import Data.Monoid ((<>)) import Data.String.Conversions (cs) import Data.Time.Clock.POSIX (getPOSIXTime) @@ -95,5 +96,9 @@ postgrest conf dbStructure pool = let handleReq = runWithClaims conf time (app dbStructure conf body) req resp <- either pgErrResponse id <$> P.use pool - (HT.run handleReq HT.ReadCommitted HT.Write) + (rollbackOnError $ HT.run handleReq HT.ReadCommitted HT.Write) respond resp + + where + rollbackOnError :: H.Session a -> H.Session a + rollbackOnError = (`catchError` ((H.sql "rollback;" >>) . throwError))