Display error descriptions
All checks were successful
Build / audit (push) Successful in 8s
Build / test (push) Successful in 14m18s

This commit is contained in:
Eugen Wissner 2024-08-20 22:36:43 +02:00
parent 42b9b671e1
commit 14cc805dcf
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 47 additions and 8 deletions

View File

@ -11,12 +11,14 @@ module SlackBuilder.Trans
import Control.Monad.Trans.Reader (ReaderT(..), asks) import Control.Monad.Trans.Reader (ReaderT(..), asks)
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as Text
import SlackBuilder.Config import SlackBuilder.Config
import Control.Monad.IO.Class (MonadIO(..)) import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Catch (MonadCatch(..), MonadThrow(..)) import Control.Monad.Catch (MonadCatch(..), MonadThrow(..))
import Control.Exception (Exception(..)) import Control.Exception (Exception(..))
import System.FilePath ((</>)) import System.FilePath ((</>))
import Text.URI (URI) import Text.URI (URI)
import qualified Text.URI as URI
import qualified Codec.Compression.Lzma as Lzma import qualified Codec.Compression.Lzma as Lzma
data SlackBuilderException data SlackBuilderException
@ -26,6 +28,33 @@ data SlackBuilderException
deriving Show deriving Show
instance Exception SlackBuilderException instance Exception SlackBuilderException
where
displayException (UpdaterNotFound updateName) = Text.unpack
$ Text.concat ["Requested package \"", updateName, "\" was not found"]
displayException (HttpsUrlExpected givenURI) = Text.unpack
$ "Only https URLs are supported, got: " <> URI.render givenURI
displayException (LzmaDecompressionFailed Lzma.LzmaRetOK) =
"Operation completed successfully"
displayException (LzmaDecompressionFailed Lzma.LzmaRetStreamEnd) =
"End of stream was reached"
displayException (LzmaDecompressionFailed Lzma.LzmaRetUnsupportedCheck) =
"Cannot calculate the integrity check"
displayException (LzmaDecompressionFailed Lzma.LzmaRetGetCheck) =
"Integrity check type is now available"
displayException (LzmaDecompressionFailed Lzma.LzmaRetMemError) =
"Cannot allocate memory"
displayException (LzmaDecompressionFailed Lzma.LzmaRetMemlimitError) =
"Memory usage limit was reached"
displayException (LzmaDecompressionFailed Lzma.LzmaRetFormatError) =
"File format not recognized"
displayException (LzmaDecompressionFailed Lzma.LzmaRetOptionsError) =
"Invalid or unsupported options"
displayException (LzmaDecompressionFailed Lzma.LzmaRetDataError) =
"Data is corrupt"
displayException (LzmaDecompressionFailed Lzma.LzmaRetBufError) =
"No progress is possible"
displayException (LzmaDecompressionFailed Lzma.LzmaRetProgError) =
"Programming error"
newtype SlackBuilderT a = SlackBuilderT newtype SlackBuilderT a = SlackBuilderT
{ runSlackBuilderT :: ReaderT Settings IO a { runSlackBuilderT :: ReaderT Settings IO a

View File

@ -6,6 +6,7 @@ module Main
( main ( main
) where ) where
import Control.Exception (Exception(..), handle)
import Data.Char (isNumber) import Data.Char (isNumber)
import Data.List.NonEmpty (NonEmpty(..)) import Data.List.NonEmpty (NonEmpty(..))
import Control.Monad.Catch (MonadThrow(..)) import Control.Monad.Catch (MonadThrow(..))
@ -20,7 +21,6 @@ import SlackBuilder.Update
import qualified Toml import qualified Toml
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as Text import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
import Control.Monad.Trans.Reader (ReaderT(..)) import Control.Monad.Trans.Reader (ReaderT(..))
import SlackBuilder.Package (PackageDescription(..)) import SlackBuilder.Package (PackageDescription(..))
import qualified SlackBuilder.Package as Package import qualified SlackBuilder.Package as Package
@ -28,6 +28,13 @@ import Text.URI.QQ (uri)
import Data.Foldable (for_, find) import Data.Foldable (for_, find)
import GHC.Records (HasField(..)) import GHC.Records (HasField(..))
import System.Process (CmdSpec(..)) import System.Process (CmdSpec(..))
import System.Console.ANSI
( setSGR
, SGR(..)
, ColorIntensity(..)
, Color(..)
, ConsoleLayer(..)
)
autoUpdatable :: [PackageDescription] autoUpdatable :: [PackageDescription]
autoUpdatable = autoUpdatable =
@ -294,17 +301,20 @@ check = for_ autoUpdatable go
>>= mapM_ checkUpdateAvailability >>= mapM_ checkUpdateAvailability
>> liftIO (putStrLn "") >> liftIO (putStrLn "")
main :: IO () main :: IO ()
main = do main = do
programCommand <- execParser slackBuilderParser programCommand <- execParser slackBuilderParser
settings <- Toml.decodeFile settingsCodec "config/config.toml" settings <- Toml.decodeFile settingsCodec "config/config.toml"
latestVersion <- flip runReaderT settings handle handleException
$ flip runReaderT settings
$ runSlackBuilderT $ runSlackBuilderT
$ executeCommand programCommand $ executeCommand programCommand
maybe (pure ()) Text.IO.putStrLn latestVersion
where where
handleException :: SlackBuilderException -> IO ()
handleException slackBuilderException
= setSGR [SetColor Foreground Dull Red]
>> putStrLn (displayException slackBuilderException)
>> setSGR [Reset]
executeCommand = \case executeCommand = \case
CheckCommand -> check >> pure Nothing CheckCommand -> check
Up2DateCommand packageName -> up2Date packageName >> pure Nothing Up2DateCommand packageName -> up2Date packageName