git.iko.soy/typeable/pandoc
on master help sign in
catch IO errors when writing media files, closes #4559 (#4619)

If we do not catch these errors, any malformed entry in a media bag could cause the loss of a whole document output. An example of malformed entry is an entry with an empty file path.

commit59f0c1d83bb573341f8ca0bf796ae41c82afd044 authorFrancesco Occhipinti <focchi.pinti@gmail.com> dateFri 4 May 2018, 19:31 (8 years ago) parent8cd3f19
2 files changed+15−1
src/Text/Pandoc/Class.hs+9−1 src/Text/Pandoc/Logging.hs+6
src/Text/Pandoc/Class.hs+9−1
@@ -477,6 +477,14 @@ liftIOError f u = do
477477 Left e -> throwError $ PandocIOError u e
478478 Right r -> return r
479479
480+-- | Show potential IO errors to the user continuing execution anyway
481+logIOError :: IO () -> PandocIO ()
482+logIOError f = do
483+ res <- liftIO $ tryIOError f
484+ case res of
485+ Left e -> report $ IgnoredIOError (E.displayException e)
486+ Right _ -> pure ()
487+
480488instance PandocMonad PandocIO where
481489 lookupEnv = liftIO . IO.lookupEnv
482490 getCurrentTime = liftIO IO.getCurrentTime
@@ -862,7 +870,7 @@ writeMedia dir mediabag subpath = do
862870 Just (_, bs) -> do
863871 report $ Extracting fullpath
864872 liftIOError (createDirectoryIfMissing True) (takeDirectory fullpath)
865− liftIOError (\p -> BL.writeFile p bs) fullpath
873+ logIOError $ BL.writeFile fullpath bs
866874
867875adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
868876adjustImagePath dir paths (Image attr lab (src, tit))
src/Text/Pandoc/Logging.hs+6
@@ -85,6 +85,7 @@ data LogMessage =
8585 | InlineNotRendered Inline
8686 | BlockNotRendered Block
8787 | DocxParserWarning String
88+ | IgnoredIOError String
8889 | CouldNotFetchResource String String
8990 | CouldNotDetermineImageSize String String
9091 | CouldNotConvertImage String String
@@ -175,6 +176,8 @@ instance ToJSON LogMessage where
175176 ["contents" .= toJSON bl]
176177 DocxParserWarning s ->
177178 ["contents" .= Text.pack s]
179+ IgnoredIOError s ->
180+ ["contents" .= Text.pack s]
178181 CouldNotFetchResource fp s ->
179182 ["path" .= Text.pack fp,
180183 "message" .= Text.pack s]
@@ -265,6 +268,8 @@ showLogMessage msg =
265268 "Not rendering " ++ show bl
266269 DocxParserWarning s ->
267270 "Docx parser warning: " ++ s
271+ IgnoredIOError s ->
272+ "IO Error (ignored): " ++ s
268273 CouldNotFetchResource fp s ->
269274 "Could not fetch resource '" ++ fp ++ "'" ++
270275 if null s then "" else ": " ++ s
@@ -332,6 +337,7 @@ messageVerbosity msg =
332337 InlineNotRendered{} -> INFO
333338 BlockNotRendered{} -> INFO
334339 DocxParserWarning{} -> INFO
340+ IgnoredIOError{} -> WARNING
335341 CouldNotFetchResource{} -> WARNING
336342 CouldNotDetermineImageSize{} -> WARNING
337343 CouldNotConvertImage{} -> WARNING