Commit d57f849993812c4cb9c5b0133a83ab8b970f4541 Parent: 9885c412167b8ec53221a90fc240db67fe26e323 Author: mcol <mcol@posteo.net> Date: 2021-10-04 10:33:53 +0100 Committer: mcol <mcol@posteo.net> Committed: 2021-10-04 10:59:48 +0100 Handle UTF8 decoding errors with leniency
src/Repositories.hs Modified
@@ -18,7 +18,8 @@ import Data.Either (fromRight) import Data.Tagged import Data.Text (pack, unpack, Text, strip, breakOn, replace, append) -import Data.Text.Encoding (decodeUtf8) +import Data.Text.Encoding (decodeUtf8With) +import Data.Text.Encoding.Error (lenientDecode) import Data.Maybe (mapMaybe) import Git import Git.Libgit2 (lgFactory, LgRepo) @@ -132,7 +133,7 @@ return $ zipWith TreeFile (fmap fst entries) contents gvalTreeEntry :: TreeEntry LgRepo -> ReaderT LgRepo IO Text -gvalTreeEntry (BlobEntry oid _) = catBlobUtf8 $ oid +gvalTreeEntry (BlobEntry oid _) = return . decodeUtf8With lenientDecode =<< catBlob oid gvalTreeEntry (TreeEntry _) = return "No contents" gvalTreeEntry (CommitEntry _) = return "No contents" @@ -178,7 +179,8 @@ treeAsLookup :: TreeFile -> Text -> Maybe (GVal m) treeAsLookup treefile = \case "path" -> Just . toGVal . treeFilePath $ treefile - "href" -> Just . toGVal . flip append ".html" . replace "/" "." . decodeUtf8 . treeFilePath $ treefile + "href" -> Just . toGVal . flip append ".html" . replace "/" "." . + decodeUtf8With lenientDecode . treeFilePath $ treefile "contents" -> Just . toGVal . treeFileContents $ treefile _ -> Nothing @@ -200,7 +202,7 @@ category = const "commit" instance Target (TreeFile) where - identify = (++ ".html") . unpack . replace "/" "." . decodeUtf8 . treeFilePath + identify = (++ ".html") . unpack . replace "/" "." . decodeUtf8With lenientDecode . treeFilePath category = const "file" genTarget