git.iko.soy/typeable/pandoc
on master help sign in
catch IO errors when writing media files, closes #4559 (#4619)
59f0c1dFrancesco Occhipinti
src/Text/Pandoc/Class.hs+9−1
@@ -477,6 +477,14 @@ liftIOError f u = do
477 Left e -> throwError $ PandocIOError u e
477 Left e -> throwError $ PandocIOError u e
478 Right r -> return r
478 Right r -> return r
479
479
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+
480instance PandocMonad PandocIO where
488instance PandocMonad PandocIO where
481 lookupEnv = liftIO . IO.lookupEnv
489 lookupEnv = liftIO . IO.lookupEnv
482 getCurrentTime = liftIO IO.getCurrentTime
490 getCurrentTime = liftIO IO.getCurrentTime
@@ -862,7 +870,7 @@ writeMedia dir mediabag subpath = do
862 Just (_, bs) -> do
870 Just (_, bs) -> do
863 report $ Extracting fullpath
871 report $ Extracting fullpath
864 liftIOError (createDirectoryIfMissing True) (takeDirectory fullpath)
872 liftIOError (createDirectoryIfMissing True) (takeDirectory fullpath)
865− liftIOError (\p -> BL.writeFile p bs) fullpath
873+ logIOError $ BL.writeFile fullpath bs
866
874
867adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
875adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
868adjustImagePath dir paths (Image attr lab (src, tit))
876adjustImagePath dir paths (Image attr lab (src, tit))
src/Text/Pandoc/Logging.hs+6
@@ -85,6 +85,7 @@ data LogMessage =
85 | InlineNotRendered Inline
85 | InlineNotRendered Inline
86 | BlockNotRendered Block
86 | BlockNotRendered Block
87 | DocxParserWarning String
87 | DocxParserWarning String
88+ | IgnoredIOError String
88 | CouldNotFetchResource String String
89 | CouldNotFetchResource String String
89 | CouldNotDetermineImageSize String String
90 | CouldNotDetermineImageSize String String
90 | CouldNotConvertImage String String
91 | CouldNotConvertImage String String
@@ -175,6 +176,8 @@ instance ToJSON LogMessage where
175 ["contents" .= toJSON bl]
176 ["contents" .= toJSON bl]
176 DocxParserWarning s ->
177 DocxParserWarning s ->
177 ["contents" .= Text.pack s]
178 ["contents" .= Text.pack s]
179+ IgnoredIOError s ->
180+ ["contents" .= Text.pack s]
178 CouldNotFetchResource fp s ->
181 CouldNotFetchResource fp s ->
179 ["path" .= Text.pack fp,
182 ["path" .= Text.pack fp,
180 "message" .= Text.pack s]
183 "message" .= Text.pack s]
@@ -265,6 +268,8 @@ showLogMessage msg =
265 "Not rendering " ++ show bl
268 "Not rendering " ++ show bl
266 DocxParserWarning s ->
269 DocxParserWarning s ->
267 "Docx parser warning: " ++ s
270 "Docx parser warning: " ++ s
271+ IgnoredIOError s ->
272+ "IO Error (ignored): " ++ s
268 CouldNotFetchResource fp s ->
273 CouldNotFetchResource fp s ->
269 "Could not fetch resource '" ++ fp ++ "'" ++
274 "Could not fetch resource '" ++ fp ++ "'" ++
270 if null s then "" else ": " ++ s
275 if null s then "" else ": " ++ s
@@ -332,6 +337,7 @@ messageVerbosity msg =
332 InlineNotRendered{} -> INFO
337 InlineNotRendered{} -> INFO
333 BlockNotRendered{} -> INFO
338 BlockNotRendered{} -> INFO
334 DocxParserWarning{} -> INFO
339 DocxParserWarning{} -> INFO
340+ IgnoredIOError{} -> WARNING
335 CouldNotFetchResource{} -> WARNING
341 CouldNotFetchResource{} -> WARNING
336 CouldNotDetermineImageSize{} -> WARNING
342 CouldNotDetermineImageSize{} -> WARNING
337 CouldNotConvertImage{} -> WARNING
343 CouldNotConvertImage{} -> WARNING