feat: log all internal database errors to stderr

This commit is contained in:
Laurence Isla
2023-11-27 23:06:40 -05:00
committed by Steve Chavez
parent 8483459d59
commit 33891e3a73
4 changed files with 32 additions and 2 deletions
+8 -2
View File
@@ -42,6 +42,7 @@ import qualified Hasql.Notifications as SQL
import qualified Hasql.Pool as SQL
import qualified Hasql.Session as SQL
import qualified Hasql.Transaction.Sessions as SQL
import qualified Network.HTTP.Types.Status as HTTP
import qualified Network.Socket as NS
import qualified PostgREST.Error as Error
import PostgREST.Version (prettyVersion)
@@ -205,11 +206,16 @@ initPool AppConfig{..} =
-- | Run an action with a database connection.
usePool :: AppState -> SQL.Session a -> IO (Either SQL.UsageError a)
usePool AppState{..} x = do
usePool appState@AppState{..} x = do
res <- SQL.use statePool x
whenLeft res (\case
SQL.AcquisitionTimeoutUsageError -> debounceLogAcquisitionTimeout -- this can happen rapidly for many requests, so we debounce
_ -> pure ())
error
-- TODO We're using the 500 HTTP status for getting all internal db errors but there's no response here. We need a new intermediate type to not rely on the HTTP status.
| Error.status (Error.PgError False error) >= HTTP.status500 -> logPgrstError appState error
| otherwise -> pure ())
return res
-- | Flush the connection pool so that any future use of the pool will
+1
View File
@@ -11,6 +11,7 @@ module PostgREST.Error
, PgError(..)
, Error(..)
, errorPayload
, status
) where
import qualified Data.Aeson as JSON