Roll back on db errors

This commit is contained in:
Joe Nelson
2016-02-22 16:52:50 -08:00
parent 7b7babd1d1
commit d4a4bbf966
2 changed files with 9 additions and 1 deletions
+3
View File
@@ -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
+6 -1
View File
@@ -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))