From 674615041a74f736f3c29e1c470dd6f645445052 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Thu, 7 Jan 2021 21:48:24 -0500 Subject: [PATCH] Correct --dump-schema swallowing error When there's no connection to pg, the following error happens when running --dump-schema: postgrest: user error (Pattern match failure in do expression at main/Main.hs:361:5-14) Now it shows a regular "could not connect to server.." error. --- main/Main.hs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/main/Main.hs b/main/Main.hs index 2c575afaf..b0a0550b0 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -28,7 +28,8 @@ import Data.Time.Clock (getCurrentTime) import Network.Wai.Handler.Warp (defaultSettings, runSettings, setHost, setPort, setServerName) import System.CPUTime (getCPUTime) -import System.IO (BufferMode (..), hSetBuffering) +import System.IO (BufferMode (..), hPrint, + hSetBuffering) import Text.Printf (hPrintf) import PostgREST.App (postgrest) @@ -345,25 +346,26 @@ reReadConfig env path refConf = do dumpSchema :: AppConfig -> IO LBS.ByteString dumpSchema conf = do - Right conn <- C.acquire . toS $ configDbUri conf - Right pgVersion <- S.run getPgVersion conn - let - getDbStructureTransaction = - HT.transaction HT.ReadCommitted HT.Read $ - getDbStructure - (toList $ configDbSchemas conf) - (configDbExtraSearchPath conf) - pgVersion - (configDbPreparedStatements conf) - result <- - timeToStderr "Loaded schema in %.3f seconds" $ - S.run getDbStructureTransaction conn - C.release conn - case result of - Left e -> do - hPutStrLn stderr $ "An error ocurred when loading the schema cache:\n" <> show e - exitFailure - Right dbStructure -> return $ Aeson.encode dbStructure + eitherConn <- C.acquire . toS $ configDbUri conf + case eitherConn of + Left e -> hPrint stderr e >> exitFailure + Right conn -> do + result <- + timeToStderr "Loaded schema in %.3f seconds" $ + flip S.run conn $ do + pgVersion <- getPgVersion + HT.transaction HT.ReadCommitted HT.Read $ + getDbStructure + (toList $ configDbSchemas conf) + (configDbExtraSearchPath conf) + pgVersion + (configDbPreparedStatements conf) + C.release conn + case result of + Left e -> do + hPutStrLn stderr $ "An error ocurred when loading the schema cache:\n" <> show e + exitFailure + Right dbStructure -> return $ Aeson.encode dbStructure -- | Print the time taken to run an IO action to stderr with the given printf string