Skip to content

Commit

Permalink
catch IO errors when writing media files, closes jgm#4559
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danse committed May 3, 2018
1 parent 5160252 commit 6d3c30a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Text/Pandoc/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,14 @@ liftIOError f u = do
Left e -> throwError $ PandocIOError u e
Right r -> return r

-- | Show potential IO errors to the user continuing execution anyway
logIOError :: IO a -> PandocIO ()
logIOError f = do
res <- liftIO $ tryIOError f
case res of
Left e -> report $ IgnoredIOError (E.displayException e)
Right _ -> pure ()

instance PandocMonad PandocIO where
lookupEnv = liftIO . IO.lookupEnv
getCurrentTime = liftIO IO.getCurrentTime
Expand Down Expand Up @@ -862,7 +870,7 @@ writeMedia dir mediabag subpath = do
Just (_, bs) -> do
report $ Extracting fullpath
liftIOError (createDirectoryIfMissing True) (takeDirectory fullpath)
liftIOError (\p -> BL.writeFile p bs) fullpath
logIOError $ BL.writeFile fullpath bs

adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
adjustImagePath dir paths (Image attr lab (src, tit))
Expand Down
6 changes: 6 additions & 0 deletions src/Text/Pandoc/Logging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ data LogMessage =
| InlineNotRendered Inline
| BlockNotRendered Block
| DocxParserWarning String
| IgnoredIOError String
| CouldNotFetchResource String String
| CouldNotDetermineImageSize String String
| CouldNotConvertImage String String
Expand Down Expand Up @@ -175,6 +176,8 @@ instance ToJSON LogMessage where
["contents" .= toJSON bl]
DocxParserWarning s ->
["contents" .= Text.pack s]
IgnoredIOError s ->
["contents" .= Text.pack s]
CouldNotFetchResource fp s ->
["path" .= Text.pack fp,
"message" .= Text.pack s]
Expand Down Expand Up @@ -265,6 +268,8 @@ showLogMessage msg =
"Not rendering " ++ show bl
DocxParserWarning s ->
"Docx parser warning: " ++ s
IgnoredIOError s ->
"IO Error (ignored): " ++ s
CouldNotFetchResource fp s ->
"Could not fetch resource '" ++ fp ++ "'" ++
if null s then "" else ": " ++ s
Expand Down Expand Up @@ -332,6 +337,7 @@ messageVerbosity msg =
InlineNotRendered{} -> INFO
BlockNotRendered{} -> INFO
DocxParserWarning{} -> INFO
IgnoredIOError{} -> WARNING
CouldNotFetchResource{} -> WARNING
CouldNotDetermineImageSize{} -> WARNING
CouldNotConvertImage{} -> WARNING
Expand Down

0 comments on commit 6d3c30a

Please sign in to comment.