From a9e2b10ac58ebf06da82477f670ce7d8ef86276d Mon Sep 17 00:00:00 2001 From: webdevred <148627186+webdevred@users.noreply.github.com> Date: Sun, 23 Nov 2025 13:58:51 +0100 Subject: [PATCH 1/2] better error messages for transformation config loading --- .../JbeamEdit/Transformation/Config.hs | 35 ++++++++++++------- src/JbeamEdit/Formatting/Config.hs | 2 +- src/JbeamEdit/IOUtils.hs | 7 ++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src-extra/transformation/JbeamEdit/Transformation/Config.hs b/src-extra/transformation/JbeamEdit/Transformation/Config.hs index 42d41202..37453fac 100644 --- a/src-extra/transformation/JbeamEdit/Transformation/Config.hs +++ b/src-extra/transformation/JbeamEdit/Transformation/Config.hs @@ -16,7 +16,13 @@ module JbeamEdit.Transformation.Config ( import Data.Scientific (Scientific) import Data.Text qualified as T -import Data.Yaml (decodeFileEither) +import Data.Yaml ( + Object, + ParseException (..), + Parser, + decodeFileEither, + prettyPrintParseException, + ) import Data.Yaml.Aeson ( FromJSON (..), withArray, @@ -107,25 +113,28 @@ instance FromJSON XGroupBreakpoints where obj pure $ XGroupBreakpoints lst +parseSupportThreshold :: Object -> Parser Double +parseSupportThreshold o = do + thr <- o .: "support-threshold" + when (thr < 1) failWithMessage $> thr + where + failWithMessage = + fail + "'support-threshold' must be a percentage value of 1 or higher (e.g., 80 or 80.8). Values below 1 (e.g., 0.80) are not allowed." + instance FromJSON TransformationConfig where parseJSON = withObject "TransformationConfig" $ \o -> TransformationConfig <$> o .:? "z-sorting-threshold" .!= defaultSortingThreshold <*> o .:? "x-group-breakpoints" .!= defaultBreakpoints - <*> o .:? "support-threshold" .!= defaultSupportThreshold + <*> parseSupportThreshold o <*> o .:? "max-support-coordinates" .!= defaultMaxSupportCoordinates +formatParseError :: ParseException -> IO () +formatParseError (AesonException err) = putErrorStringLn err +formatParseError excp = putErrorStringLn (prettyPrintParseException excp) + loadTransformationConfig :: FilePath -> IO TransformationConfig loadTransformationConfig filename = do res <- decodeFileEither filename - case res of - Right tc -> pure tc - Left err -> do - putErrorLine $ show err - pure - ( TransformationConfig - defaultSortingThreshold - defaultBreakpoints - defaultSupportThreshold - defaultMaxSupportCoordinates - ) + either ((newTransformationConfig <$) . formatParseError) pure res diff --git a/src/JbeamEdit/Formatting/Config.hs b/src/JbeamEdit/Formatting/Config.hs index 116358e2..7924fe25 100644 --- a/src/JbeamEdit/Formatting/Config.hs +++ b/src/JbeamEdit/Formatting/Config.hs @@ -71,7 +71,7 @@ readFormattingConfig maybeJbflPath = do configDir <- getConfigDir case maybeJbflPath of Just jbfl -> - putErrorLine $ "Loading jbfl: " <> pack jbfl + putErrorStringLn $ "Loading jbfl: " ++ jbfl Nothing -> createRuleFileIfDoesNotExist (configDir "rules.jbfl") configPath <- getConfigPath maybeJbflPath configDir diff --git a/src/JbeamEdit/IOUtils.hs b/src/JbeamEdit/IOUtils.hs index af45cc48..e3eb9442 100644 --- a/src/JbeamEdit/IOUtils.hs +++ b/src/JbeamEdit/IOUtils.hs @@ -1,4 +1,4 @@ -module JbeamEdit.IOUtils (tryReadFile, putErrorLine, reportInvalidNodes) where +module JbeamEdit.IOUtils (tryReadFile, putErrorLine, putErrorStringLn, reportInvalidNodes) where import Control.Exception (IOException, try) import Control.Monad (unless) @@ -13,8 +13,11 @@ import JbeamEdit.Core.Node (Node) import JbeamEdit.Formatting (formatNode, newRuleSet) import System.IO (hPutStrLn, stderr) +putErrorStringLn :: String -> IO () +putErrorStringLn = hPutStrLn stderr + putErrorLine :: Text -> IO () -putErrorLine = hPutStrLn stderr . T.unpack +putErrorLine = putErrorStringLn . T.unpack reportInvalidNodes :: Text -> [Node] -> IO () reportInvalidNodes msg nodes = From 5eac8871dba86331dfd13b66c546d4cc8ac447e0 Mon Sep 17 00:00:00 2001 From: webdevred <148627186+webdevred@users.noreply.github.com> Date: Sun, 23 Nov 2025 14:05:55 +0100 Subject: [PATCH 2/2] Removed redundant import --- src/JbeamEdit/Formatting/Config.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/JbeamEdit/Formatting/Config.hs b/src/JbeamEdit/Formatting/Config.hs index 7924fe25..8d29aab4 100644 --- a/src/JbeamEdit/Formatting/Config.hs +++ b/src/JbeamEdit/Formatting/Config.hs @@ -2,7 +2,6 @@ module JbeamEdit.Formatting.Config (readFormattingConfig, copyToConfigDir, ConfigType (..)) where -import Data.Text (pack) import GHC.IO.Exception (IOErrorType (NoSuchThing)) import JbeamEdit.Formatting.Rules import JbeamEdit.IOUtils