git.iko.soy/typeable/pandoc
on master help sign in
UTF8: Better error message for invalid UTF8.

Read bytestring and use Text's decodeUtf8 instead of using System.IO's hGetContents. This way you get a message saying "invalid UTF-8 stream" instead of "invalid byte sequence." You are also told which byte caused the problem.

commitc80ffe9171391f1a15d0c4b069f4b994c1aef050 authorJohn MacFarlane <fiddlosopher@gmail.com> dateWed 26 Sep 2012, 09:04 (14 years ago) parent885ef20
1 file changed+6−4
src/Text/Pandoc/UTF8.hs+6−4
src/Text/Pandoc/UTF8.hs+6−4
@@ -52,7 +52,8 @@ import Codec.Binary.UTF8.String (encodeString, decodeString)
5252
5353import System.IO hiding (readFile, writeFile, getContents,
5454 putStr, putStrLn, hPutStr, hPutStrLn, hGetContents)
55−import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn )
55+import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn,
56+ catch)
5657import qualified System.IO as IO
5758import qualified Data.ByteString.Char8 as B
5859import qualified Data.ByteString.Lazy as BL
@@ -86,9 +87,10 @@ hPutStrLn :: Handle -> String -> IO ()
8687hPutStrLn h s = hSetEncoding h utf8 >> IO.hPutStrLn h s
8788
8889hGetContents :: Handle -> IO String
89−hGetContents h = hSetEncoding h utf8_bom
90− >> hSetNewlineMode h universalNewlineMode
91− >> IO.hGetContents h
90+hGetContents h = fmap (TL.unpack . TL.decodeUtf8) $ BL.hGetContents h
91+-- hGetContents h = hSetEncoding h utf8_bom
92+-- >> hSetNewlineMode h universalNewlineMode
93+-- >> IO.hGetContents h
9294
9395toString :: B.ByteString -> String
9496toString = T.unpack . T.decodeUtf8With lenientDecode