From 1418e0ae46d3805c20796b0ff7896881658798df Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 8 Sep 2024 16:44:57 +0200 Subject: [PATCH] Pretty print settings parsing errors --- src/Main.hs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index ac0b474..375ee8c 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -6,7 +6,7 @@ module Main ( main ) where -import Control.Exception (Exception(..), handle) +import Control.Exception (Exception(..), handle, SomeException(..)) import Data.Char (isNumber) import Control.Monad.Catch (MonadThrow(..)) import Control.Monad.IO.Class (MonadIO(..)) @@ -20,6 +20,7 @@ import SlackBuilder.Update import qualified Toml import Data.Text (Text) import qualified Data.Text as Text +import qualified Data.Text.IO as Text import Control.Monad.Trans.Reader (ReaderT(..)) import SlackBuilder.Package (PackageDescription(..)) import qualified SlackBuilder.Package as Package @@ -258,15 +259,22 @@ check = for_ autoUpdatable go >> liftIO (putStrLn "") main :: IO () -main = do - programCommand <- execParser slackBuilderParser - settings <- Toml.decodeFile settingsCodec "config/config.toml" - handle handleException - $ flip runReaderT settings - $ runSlackBuilderT - $ executeCommand programCommand +main = execParser slackBuilderParser + >>= handle handleException . withCommandLine where - handleException :: SlackBuilderException -> IO () + withCommandLine programCommand = do + settingsResult <- Toml.decodeFileEither settingsCodec configurationFile + case settingsResult of + Right settings -> flip runReaderT settings + $ runSlackBuilderT + $ executeCommand programCommand + Left settingsErrors + -> setSGR [SetColor Foreground Dull Red] + >> putStrLn (configurationFile <> " parsing failed.") + >> setSGR [Reset] + >> Text.putStr (Toml.prettyTomlDecodeErrors settingsErrors) + configurationFile = "config/config.toml" + handleException :: SomeException -> IO () handleException slackBuilderException = setSGR [SetColor Foreground Dull Red] >> putStrLn (displayException slackBuilderException)