More polymorphic hoist functions

This commit is contained in:
Tomasz Rybarczyk 2018-12-22 18:32:24 +01:00
parent 07e1c0924b
commit bd055928ae

View File

@ -16,7 +16,7 @@ module Database.PostgreSQL.PG
import Prelude import Prelude
import Control.Monad.Error.Class (catchError, throwError) import Control.Monad.Error.Class (class MonadError, catchError, throwError)
import Control.Monad.Except (ExceptT(..)) import Control.Monad.Except (ExceptT(..))
import Control.Monad.Except.Trans (ExceptT, runExceptT) import Control.Monad.Except.Trans (ExceptT, runExceptT)
import Data.Either (Either(..)) import Data.Either (Either(..))
@ -42,13 +42,14 @@ type Database = String
-- | PGError a)`. -- | PGError a)`.
type PG a = ExceptT PGError Aff a type PG a = ExceptT PGError Aff a
hoistWith :: forall e m. MonadAff m => (PGError -> e) -> PG ~> ExceptT e m hoistWith :: forall e m. MonadAff m => MonadError e m => (PGError -> e) -> PG ~> ExceptT e m
hoistWith f m = ExceptT $ liftAff $ hoistWith f m = do
runExceptT m >>= case _ of result <- liftAff $ runExceptT m
Right a -> pure (Right a) case result of
Left pgError -> pure (Left (f pgError)) Right a -> pure a
Left pgError -> throwError (f pgError)
hoist :: forall m. MonadAff m => PG ~> ExceptT PGError m hoist :: forall m. MonadAff m => MonadError PGError m => PG ~> ExceptT PGError m
hoist = hoistWith identity hoist = hoistWith identity
-- | Run an action with a connection. The connection is released to the pool -- | Run an action with a connection. The connection is released to the pool