imp: simplify the unicode decode error message; refactor

This commit is contained in:
Simon Michael 2025-05-30 09:43:13 -10:00
parent 296814fd49
commit 713773cfc3

View File

@ -303,17 +303,15 @@ exitOnError = flip catches
-- But there are many variant wordings and they probably change over time. -- But there are many variant wordings and they probably change over time.
-- It's not ideal. -- It's not ideal.
isUnicodeError :: Exception e => e -> Bool isUnicodeError :: Exception e => e -> Bool
isUnicodeError ex = any (`isInfixOf` msg) unicodeerrorpatterns isUnicodeError ex =
where let msg = map toLower (show ex) in any (`isInfixOf` msg) [
msg = map toLower $ show ex "illegal byte sequence"
unicodeerrorpatterns = [ , "invalid byte sequence"
"illegal byte sequence" , "cannot decode byte sequence"
, "invalid byte sequence" , "invalid character"
, "cannot decode byte sequence" , "invalid or incomplete multibyte"
, "invalid character" , "mkTextEncoding: invalid argument"
, "invalid or incomplete multibyte" ]
, "mkTextEncoding: invalid argument"
]
exitUnicode :: Exception e => e -> IO () exitUnicode :: Exception e => e -> IO ()
exitUnicode ex = do exitUnicode ex = do
@ -322,16 +320,15 @@ exitOnError = flip catches
noencoding = map toLower enc == "ascii" noencoding = map toLower enc == "ascii"
msg = unlines $ [ msg = unlines $ [
rstrip $ show ex rstrip $ show ex
, "Some text could not be decoded/encoded with the system text encoding: " <> enc , "Some text could not be decoded with the system text encoding, " <> enc <> "."
] ++ ] ++
if noencoding if noencoding
then [ then [
"Please configure a system locale with a text encoding to handle non-ascii text" "Please configure a system locale which can decode this text."
] ]
else [ else [
-- advice suitable for programs which always use the system text encoding: "Please either convert the text to this encoding,"
"Please convert all data to the system encoding (eg with iconv)," , "or configure a system locale which can decode this text."
, "or configure the system encoding to match your data (eg by setting LANG)."
] ]
exitWithErrorMessage msg exitWithErrorMessage msg