From 4587eccdf450f452ecb4df5332767492232b2c6a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 29 May 2025 12:25:06 -1000 Subject: [PATCH] ;fix: recognise another case of text decoding failure --- hledger-lib/Hledger/Utils/IO.hs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Utils/IO.hs b/hledger-lib/Hledger/Utils/IO.hs index 2babb8bf9..2cc13c83e 100644 --- a/hledger-lib/Hledger/Utils/IO.hs +++ b/hledger-lib/Hledger/Utils/IO.hs @@ -291,19 +291,25 @@ exitWithErrorMessage msg = printError msg >> exitFailure -- exitOnError :: IO () -> IO () exitOnError = flip catches - [Handler (\(e::UnicodeException) -> exitUnicode e) + [-- Handler (\(e::SomeException) -> error' $ pshow e), -- debug + Handler (\(e::UnicodeException) -> exitUnicode e) ,Handler (\(e::IOException) -> if isUnicodeError e then exitUnicode e else exitOther e) ,Handler (\(e::ErrorCall) -> exitOther e) ] where + -- Many decoding failures do not produce a UnicodeException, unfortunately. + -- So this fragile hack detects them from the error message. + -- But there are many variant wordings and they probably change over time. + -- It's not ideal. isUnicodeError :: Exception e => e -> Bool isUnicodeError ex = any (`isInfixOf` msg) unicodeerrorpatterns where msg = map toLower $ show ex - unicodeerrorpatterns = [ -- keep updated + unicodeerrorpatterns = [ "illegal byte sequence" , "invalid byte sequence" + , "cannot decode byte sequence" , "invalid character" , "invalid or incomplete multibyte" , "mkTextEncoding: invalid argument"