@@ -95,6 +95,7 @@ import Text.Pandoc.MIME (getMimeType)
9595import System.FilePath ( (</>), takeExtension, dropExtension )
9696import Data.Generics (Typeable, Data)
9797import qualified Control.Monad.State as S
98+import qualified Control.Exception as E
9899import Control.Monad (msum, unless)
99100import Text.Pandoc.Pretty (charWidth)
100101import System.Locale (defaultTimeLocale)
@@ -586,12 +587,13 @@ readDataFileUTF8 userDir fname =
586587
587588-- | Fetch an image or other item from the local filesystem or the net.
588589-- Returns raw content and maybe mime type.
589−fetchItem :: String -> String -> IO (BS.ByteString, Maybe String)
590+fetchItem :: String -> String
591+ -> IO (Either E.SomeException (BS.ByteString, Maybe String))
590592fetchItem sourceDir s =
591593 case s of
592594 _ | isAbsoluteURI s -> openURL s
593595 | isAbsoluteURI sourceDir -> openURL $ sourceDir ++ "/" ++ s
594− | otherwise -> do
596+ | otherwise -> E.try $ do
595597 let mime = case takeExtension s of
596598 ".gz" -> getMimeType $ dropExtension s
597599 x -> getMimeType x
@@ -600,21 +602,21 @@ fetchItem sourceDir s =
600602 return (cont, mime)
601603
602604-- | Read from a URL and return raw data and maybe mime type.
603−openURL :: String -> IO (BS.ByteString, Maybe String)
605+openURL :: String -> IO (Either E.SomeException (BS.ByteString, Maybe String))
604606openURL u
605607 | "data:" `isPrefixOf` u =
606608 let mime = takeWhile (/=',') $ drop 5 u
607609 contents = B8.pack $ unEscapeString $ drop 1 $ dropWhile (/=',') u
608− in return (contents, Just mime)
610+ in return $ Right (contents, Just mime)
609611#ifdef HTTP_CONDUIT
610− | otherwise = do
612+ | otherwise = E.try $ do
611613 req <- parseUrl u
612614 resp <- withManager $ httpLbs req
613615 return (BS.concat $ toChunks $ responseBody resp,
614616 UTF8.toString `fmap` lookup hContentType (responseHeaders resp))
615617#else
616− | otherwise = getBodyAndMimeType `fmap` browse
617− (do S.liftIO $ UTF8.hPutStrLn stderr $ "Fetching " ++ u ++ "..."
618+ | otherwise = E.try $ getBodyAndMimeType `fmap` browse
619+ (do UTF8.hPutStrLn stderr $ "Fetching " ++ u ++ "..."
618620 setOutHandler $ const (return ())
619621 setAllowRedirects True
620622 request (getRequest' u'))