diff --git a/flake.lock b/flake.lock index d983f874b..4d5dd803d 100644 --- a/flake.lock +++ b/flake.lock @@ -370,11 +370,11 @@ "tinylog": "tinylog" }, "locked": { - "lastModified": 1734090572, - "narHash": "sha256-DwvTuZFjrStKYOxZn99s7Fk89AZQskQ0593AexqY7lc=", + "lastModified": 1734428345, + "narHash": "sha256-Nbss1aAIfrgNMinY0AOfaOa8EUXWhAxHrw7Fhyb55d0=", "owner": "nammayatri", "repo": "euler-hs", - "rev": "dfe2249ca40fbee35b772eaa937c762498309312", + "rev": "bccffdbc6c27e9ed648a1abb95f288763cff6bcc", "type": "github" }, "original": { diff --git a/lib/mobility-core/src/Kernel/Storage/Esqueleto/Transactionable.hs b/lib/mobility-core/src/Kernel/Storage/Esqueleto/Transactionable.hs index e525e94d1..a7d3395e2 100644 --- a/lib/mobility-core/src/Kernel/Storage/Esqueleto/Transactionable.hs +++ b/lib/mobility-core/src/Kernel/Storage/Esqueleto/Transactionable.hs @@ -14,9 +14,11 @@ module Kernel.Storage.Esqueleto.Transactionable where +import qualified Data.Pool as DP import Database.Esqueleto.Experimental (runSqlPool) import Database.Persist.Postgresql (runSqlPoolNoTransaction) -import Kernel.Prelude +import qualified EulerHS.Types as ET +import Kernel.Prelude hiding (either) import Kernel.Storage.Esqueleto.Config import Kernel.Storage.Esqueleto.DTypeBuilder import Kernel.Storage.Esqueleto.Logger @@ -24,6 +26,7 @@ import Kernel.Storage.Esqueleto.SqlDB import Kernel.Types.Logging import Kernel.Types.Time (getCurrentTime) import Kernel.Utils.IOLogging (LoggerEnv) +import Kernel.Utils.Logging (logTagError) type Transactionable m = Transactionable' SelectSqlDB m @@ -76,7 +79,16 @@ runTransactionIO logEnv dbEnv (SqlDB run) = do SqlDBEnv { currentTime = now } - runLoggerIO logEnv $ runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool + runLoggerIO logEnv $ do + res <- try @_ @SomeException $ runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool + fromEitherOrM res $ \e -> do + let res' = transformException e + logTagError "DB QUERY" $ "Transformed ERROR response: " <> show res' + case res' of + ET.DBError (ET.SQLError (ET.PostgresError (ET.PostgresSqlError "" ET.PostgresFatalError "" "" ""))) _ -> do + liftIO $ DP.destroyAllResources dbEnv.connPool + runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool + _ -> throwIO e runInReplica :: (EsqDBReplicaFlow m r, MonadThrow m, Log m) => SelectSqlDB a -> m a runInReplica (SelectSqlDB m) = do @@ -104,4 +116,26 @@ runNoTransactionIO logEnv dbEnv (SqlDB run) = do SqlDBEnv { currentTime = now } - runLoggerIO logEnv $ runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing + runLoggerIO logEnv $ do + res <- try @_ @SomeException $ runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing + fromEitherOrM res $ \e -> do + let res' = transformException e + logTagError "DB QUERY" $ "Transformed ERROR response: " <> show res' + case res' of + ET.DBError (ET.SQLError (ET.PostgresError (ET.PostgresSqlError "" ET.PostgresFatalError "" "" ""))) _ -> do + liftIO $ DP.destroyAllResources dbEnv.connPool + runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing + _ -> throwIO e + +transformException :: SomeException -> ET.DBError +transformException e = + maybe + (ET.DBError ET.UnrecognizedError $ show e) + (ET.postgresErrorToDbError (show e)) + $ fromException e + +fromEitherOrM :: Monad m => Either e a -> (e -> m a) -> m a +fromEitherOrM either f = + case either of + Right x -> pure x + Left e -> f e