git.iko.soy/typeable/pandoc
on master help sign in
Use PandocIOError in Class.
commite08e93e84401306dd87e4a96d4155182da5193d9 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 23 Feb 2017, 16:21 (10 years ago) parentde5102a
2 files changed+24−41
src/Text/Pandoc/App.hs+1−5 src/Text/Pandoc/Class.hs+23−36
src/Text/Pandoc/App.hs+1−5
@@ -725,11 +725,7 @@ readSource src = case parseURI src of
725725 _ -> liftIO $ UTF8.readFile src
726726
727727readURI :: MonadIO m => FilePath -> m String
728−readURI src = do
729− res <- liftIO $ openURL src
730− case res of
731− Left e -> liftIO $ throwIO e
732− Right (bs,_) -> return $ UTF8.toString bs
728+readURI src = liftIO $ (UTF8.toString . fst) <$> openURL src
733729
734730readFile' :: MonadIO m => FilePath -> m B.ByteString
735731readFile' "-" = liftIO $ B.getContents
src/Text/Pandoc/Class.hs+23−36
@@ -115,9 +115,7 @@ class (Functor m, Applicative m, Monad m, MonadError PandocError m)
115115 openURL :: String -> m (B.ByteString, Maybe MimeType)
116116 readFileLazy :: FilePath -> m BL.ByteString
117117 readFileStrict :: FilePath -> m B.ByteString
118− readDataFile :: Maybe FilePath
119− -> FilePath
120− -> m B.ByteString
118+ readDataFile :: Maybe FilePath -> FilePath -> m B.ByteString
121119 glob :: String -> m [FilePath]
122120 getModificationTime :: FilePath -> m UTCTime
123121 getCommonState :: m CommonState
@@ -218,40 +216,25 @@ newtype PandocIO a = PandocIO {
218216 , MonadError PandocError
219217 )
220218
219+liftIOError :: (String -> IO a) -> String -> PandocIO a
220+liftIOError f u = do
221+ res <- liftIO $ tryIOError $ f u
222+ case res of
223+ Left e -> throwError $ PandocIOError u e
224+ Right r -> return r
225+
221226instance PandocMonad PandocIO where
222227 lookupEnv = liftIO . IO.lookupEnv
223228 getCurrentTime = liftIO IO.getCurrentTime
224229 getCurrentTimeZone = liftIO IO.getCurrentTimeZone
225230 newStdGen = liftIO IO.newStdGen
226231 newUniqueHash = hashUnique <$> (liftIO IO.newUnique)
227− openURL u = do
228− eitherRes <- liftIO $ (tryIOError $ IO.openURL u)
229− case eitherRes of
230− Right (Right res) -> return res
231− Right (Left _) -> throwError $ PandocFileReadError u
232− Left _ -> throwError $ PandocFileReadError u
233− readFileLazy s = do
234− eitherBS <- liftIO (tryIOError $ BL.readFile s)
235− case eitherBS of
236− Right bs -> return bs
237− Left _ -> throwError $ PandocFileReadError s
238− readFileStrict s = do
239− eitherBS <- liftIO (tryIOError $ B.readFile s)
240− case eitherBS of
241− Right bs -> return bs
242− Left _ -> throwError $ PandocFileReadError s
243− -- TODO: Make this more sensitive to the different sorts of failure
244− readDataFile mfp fname = do
245− eitherBS <- liftIO (tryIOError $ IO.readDataFile mfp fname)
246− case eitherBS of
247− Right bs -> return bs
248− Left _ -> throwError $ PandocFileReadError fname
232+ openURL u = liftIOError IO.openURL u
233+ readFileLazy s = liftIOError BL.readFile s
234+ readFileStrict s = liftIOError B.readFile s
235+ readDataFile mfp fname = liftIOError (IO.readDataFile mfp) fname
249236 glob = liftIO . IO.glob
250− getModificationTime fp = do
251− eitherMtime <- liftIO (tryIOError $ IO.getModificationTime fp)
252− case eitherMtime of
253− Right mtime -> return mtime
254− Left _ -> throwError $ PandocFileReadError fp
237+ getModificationTime fp = liftIOError IO.getModificationTime fp
255238 getCommonState = PandocIO $ lift get
256239 putCommonState x = PandocIO $ lift $ put x
257240 logOutput msg =
@@ -304,8 +287,8 @@ downloadOrRead sourceURL s = do
304287 readLocalFile $ dropWhile (=='/') (uriPath u')
305288 _ -> readLocalFile fp -- get from local file system
306289 where readLocalFile f = do
307− cont <- readFileStrict f
308− return (cont, mime)
290+ cont <- readFileStrict f
291+ return (cont, mime)
309292 httpcolon = URI{ uriScheme = "http:",
310293 uriAuthority = Nothing,
311294 uriPath = "",
@@ -418,17 +401,20 @@ instance PandocMonad PandocPure where
418401 modifyPureState $ \st -> st { stUniqStore = us }
419402 return u
420403 _ -> M.fail "uniq store ran out of elements"
421− openURL _ = throwError $ PandocSomeError "Cannot open URL in PandocPure"
404+ openURL u = throwError $ PandocIOError u $
405+ userError "Cannot open URL in PandocPure"
422406 readFileLazy fp = do
423407 fps <- getsPureState stFiles
424408 case infoFileContents <$> getFileInfo fp fps of
425409 Just bs -> return (BL.fromStrict bs)
426− Nothing -> throwError $ PandocFileReadError fp
410+ Nothing -> throwError $ PandocIOError fp
411+ (userError "File not found in PureState")
427412 readFileStrict fp = do
428413 fps <- getsPureState stFiles
429414 case infoFileContents <$> getFileInfo fp fps of
430415 Just bs -> return bs
431− Nothing -> throwError $ PandocFileReadError fp
416+ Nothing -> throwError $ PandocIOError fp
417+ (userError "File not found in PureState")
432418 readDataFile Nothing "reference.docx" = do
433419 (B.concat . BL.toChunks . fromArchive) <$> getsPureState stReferenceDocx
434420 readDataFile Nothing "reference.odt" = do
@@ -450,7 +436,8 @@ instance PandocMonad PandocPure where
450436 fps <- getsPureState stFiles
451437 case infoFileMTime <$> (getFileInfo fp fps) of
452438 Just tm -> return tm
453− Nothing -> throwError $ PandocFileReadError fp
439+ Nothing -> throwError $ PandocIOError fp
440+ (userError "Can't get modification time")
454441
455442 getCommonState = PandocPure $ lift $ get
456443 putCommonState x = PandocPure $ lift $ put x