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.
This commit is contained in:
steve-chavez
2021-01-19 13:49:40 -05:00
committed by Steve Chavez
parent 15039553db
commit 674615041a
+22 -20
View File
@@ -28,7 +28,8 @@ import Data.Time.Clock (getCurrentTime)
import Network.Wai.Handler.Warp (defaultSettings, runSettings, import Network.Wai.Handler.Warp (defaultSettings, runSettings,
setHost, setPort, setServerName) setHost, setPort, setServerName)
import System.CPUTime (getCPUTime) import System.CPUTime (getCPUTime)
import System.IO (BufferMode (..), hSetBuffering) import System.IO (BufferMode (..), hPrint,
hSetBuffering)
import Text.Printf (hPrintf) import Text.Printf (hPrintf)
import PostgREST.App (postgrest) import PostgREST.App (postgrest)
@@ -345,25 +346,26 @@ reReadConfig env path refConf = do
dumpSchema :: AppConfig -> IO LBS.ByteString dumpSchema :: AppConfig -> IO LBS.ByteString
dumpSchema conf = dumpSchema conf =
do do
Right conn <- C.acquire . toS $ configDbUri conf eitherConn <- C.acquire . toS $ configDbUri conf
Right pgVersion <- S.run getPgVersion conn case eitherConn of
let Left e -> hPrint stderr e >> exitFailure
getDbStructureTransaction = Right conn -> do
HT.transaction HT.ReadCommitted HT.Read $ result <-
getDbStructure timeToStderr "Loaded schema in %.3f seconds" $
(toList $ configDbSchemas conf) flip S.run conn $ do
(configDbExtraSearchPath conf) pgVersion <- getPgVersion
pgVersion HT.transaction HT.ReadCommitted HT.Read $
(configDbPreparedStatements conf) getDbStructure
result <- (toList $ configDbSchemas conf)
timeToStderr "Loaded schema in %.3f seconds" $ (configDbExtraSearchPath conf)
S.run getDbStructureTransaction conn pgVersion
C.release conn (configDbPreparedStatements conf)
case result of C.release conn
Left e -> do case result of
hPutStrLn stderr $ "An error ocurred when loading the schema cache:\n" <> show e Left e -> do
exitFailure hPutStrLn stderr $ "An error ocurred when loading the schema cache:\n" <> show e
Right dbStructure -> return $ Aeson.encode dbStructure exitFailure
Right dbStructure -> return $ Aeson.encode dbStructure
-- | Print the time taken to run an IO action to stderr with the given printf string -- | Print the time taken to run an IO action to stderr with the given printf string