diff --git a/hledger-lib/Hledger/Data/Balancing.hs b/hledger-lib/Hledger/Data/Balancing.hs index 3262e8c2e..6c59b4724 100644 --- a/hledger-lib/Hledger/Data/Balancing.hs +++ b/hledger-lib/Hledger/Data/Balancing.hs @@ -18,7 +18,6 @@ module Hledger.Data.Balancing , isTransactionBalanced , balanceTransaction , balanceTransactionHelper -, annotateErrorWithTransaction -- * journal balancing , journalBalanceTransactions , journalCheckBalanceAssertions @@ -36,7 +35,7 @@ import Data.Foldable (asum) import Data.Function ((&)) import qualified Data.HashTable.Class as H (toList) import qualified Data.HashTable.ST.Cuckoo as H -import Data.List (intercalate, partition, sortOn) +import Data.List (partition, sortOn) import Data.List.Extra (nubSort) import Data.Maybe (fromJust, fromMaybe, isJust, isNothing, mapMaybe) import qualified Data.Set as S @@ -163,15 +162,21 @@ balanceTransactionHelper bopts t = do -- | Generate a transaction balancing error message, given the transaction -- and one or more suberror messages. transactionBalanceError :: Transaction -> [String] -> String -transactionBalanceError t errs = - annotateErrorWithTransaction t $ - intercalate "\n" $ "could not balance this transaction:" : errs - -annotateErrorWithTransaction :: Transaction -> String -> String -annotateErrorWithTransaction t s = - unlines [ sourcePosPairPretty $ tsourcepos t, s - , T.unpack . T.stripEnd $ showTransaction t - ] +transactionBalanceError t errs = printf (unlines + [ "unbalanced transaction: %s:", + "%s", + "\n%s" + ]) + (sourcePosPairPretty $ tsourcepos t) + (textChomp ex) + (chomp $ unlines errs) + where + (_f,_l,_mcols,ex) = makeTransactionErrorExcerpt t finderrcols + where + finderrcols _ = Nothing + -- finderrcols t = Just (1, Just w) + -- where + -- w = maximumDef 1 $ map T.length $ T.lines $ showTransaction t -- | Infer up to one missing amount for this transactions's real postings, and -- likewise for its balanced virtual postings, if needed; or return an error diff --git a/hledger/test/csv.test b/hledger/test/csv.test index 6368db666..05f688c44 100644 --- a/hledger/test/csv.test +++ b/hledger/test/csv.test @@ -1039,7 +1039,7 @@ account1 assets:bank:checking fields date, description, account2, amount $ ./csvtest.sh ->2 /could not balance this transaction/ +>2 /unbalanced transaction/ >=1 ## . diff --git a/hledger/test/errors/README.md b/hledger/test/errors/README.md index c1dda4835..07155b2be 100644 --- a/hledger/test/errors/README.md +++ b/hledger/test/errors/README.md @@ -46,8 +46,8 @@ Here is the current status |--------------------------|------------|------|-----------|---------|----------|-----------------| | accounts | ✓ | ✓ | ✓ | ✓✓ | | | | assertions | ✓ | ✓ | ✓ | ✓✓ | | | -| balanced | | ✓ | - | ✓ | | | -| balancednoautoconversion | | ✓ | - | ✓ | | | +| balanced | ✓ | ✓ | - | ✓ | | | +| balancednoautoconversion | ✓ | ✓ | - | ✓ | | | | commodities | ✓ | ✓ | ✓(approx) | ✓✓ | | | | ordereddates | ✓ | ✓ | ✓ | ✓✓ | | | | parseable | ✓ | ✓ | ✓ | ✓✓ | | | diff --git a/hledger/test/errors/balanced.test b/hledger/test/errors/balanced.test index 0ac26e501..73f0c87b2 100644 --- a/hledger/test/errors/balanced.test +++ b/hledger/test/errors/balanced.test @@ -1,9 +1,9 @@ -$$$ hledger check -f balanced.j ->>>2 /hledger: Error: .*balanced.j:3-4 -could not balance this transaction: +$ hledger check -f balanced.j +>2 /hledger: Error: unbalanced transaction: .*balanced.j:3-4: +3 | 2022-01-01 + | a 1 + real postings' sum should be 0 but is: 1 -2022-01-01 - a 1 / ->>>= 1 \ No newline at end of file +>= 1 \ No newline at end of file diff --git a/hledger/test/errors/balancednoautoconversion.test b/hledger/test/errors/balancednoautoconversion.test index 69ad32145..94de0f47e 100644 --- a/hledger/test/errors/balancednoautoconversion.test +++ b/hledger/test/errors/balancednoautoconversion.test @@ -1,11 +1,11 @@ -$$$ hledger check balancednoautoconversion -f balancednoautoconversion.j ->>>2 /hledger: Error: .*balancednoautoconversion.j:6-8 -could not balance this transaction: +$ hledger check balancednoautoconversion -f balancednoautoconversion.j +>2 /hledger: Error: unbalanced transaction: .*balancednoautoconversion.j:6-8: +6 | 2022-01-01 + | a 1 A + | b -1 B + real postings' sum should be 0 but is: 1 A -1 B -2022-01-01 - a 1 A - b -1 B / ->>>= 1 \ No newline at end of file +>= 1 \ No newline at end of file diff --git a/hledger/test/journal/auto-postings.test b/hledger/test/journal/auto-postings.test index 8631e0fa6..2ff2860b0 100644 --- a/hledger/test/journal/auto-postings.test +++ b/hledger/test/journal/auto-postings.test @@ -323,7 +323,7 @@ $ hledger -f- register --auto # 16. No, transaction must be balanced both with and without auto postings. $ hledger -f- print -x --auto ->2 /could not balance/ +>2 /unbalanced/ >=1 # 17. Commodity display styles are applied to auto posting amounts. diff --git a/hledger/test/journal/parse-errors.test b/hledger/test/journal/parse-errors.test index 57ad081bd..47b58101e 100644 --- a/hledger/test/journal/parse-errors.test +++ b/hledger/test/journal/parse-errors.test @@ -123,12 +123,12 @@ $ hledger -f- print b 1B $ hledger -f- print >2 -hledger: Error: -:1-3 -could not balance this transaction: +hledger: Error: unbalanced transaction: -:1-3: +1 | 2020-01-01 + | a 1A + | b 1B + real postings all have the same sign -2020-01-01 - a 1A - b 1B >=1