diff options
author | Jesse Rosenthal <jrosenthal@jhu.edu> | 2016-11-26 23:43:54 -0500 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-01-25 17:07:39 +0100 |
commit | 23c5b0d0f1901aa3ab68391f927de4f5278b5942 (patch) | |
tree | 9b96e22a6135b0aae62ae498f4eebbf1dc990e8a /src/Text/Pandoc/Class.hs | |
parent | 04487779b26458597fb751325b24c576b5088662 (diff) | |
download | pandoc-23c5b0d0f1901aa3ab68391f927de4f5278b5942.tar.gz |
Implement Errors in PandocMonad
Errors can be thrown purely with `throwError`. At the moment there are
only three kinds of errors:
1. PandocFileReadError FilePath (for problems reading a file from the
filesystem)
2. PandocShouldNeverHappenError String (for stuff that should never
happen but we need to pattern-match anyway)
3. PandocSomeError String (a grab bag of everything else)
Of course, we need to subdivide the third item in this list.
Diffstat (limited to 'src/Text/Pandoc/Class.hs')
-rw-r--r-- | src/Text/Pandoc/Class.hs | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs index 64fd7e907..69d2bb761 100644 --- a/src/Text/Pandoc/Class.hs +++ b/src/Text/Pandoc/Class.hs @@ -107,8 +107,10 @@ getPOSIXTime = utcTimeToPOSIXSeconds <$> getCurrentTime -- We can add to this as we go -data PandocExecutionError = PandocFileReadError String - deriving (Show, Typeable) +data PandocExecutionError = PandocFileReadError FilePath + | PandocShouldNeverHappenError String + | PandocSomeError String + deriving (Show, Typeable) -- Nothing in this for now, but let's put it there anyway. data PandocStateIO = PandocStateIO @@ -125,7 +127,9 @@ runIOorExplode ma = do eitherVal <- runIO ma case eitherVal of Right x -> return x - Left (PandocFileReadError s) -> error s + Left (PandocFileReadError fp) -> error $ "promple reading " ++ fp + Left (PandocShouldNeverHappenError s) -> error s + Left (PandocSomeError s) -> error s newtype PandocIO a = PandocIO { unPandocIO :: ExceptT PandocExecutionError (StateT PandocStateIO IO) a @@ -142,13 +146,13 @@ instance PandocMonad PandocIO where eitherBS <- liftIO (tryIOError $ BL.readFile s) case eitherBS of Right bs -> return bs - Left _ -> throwError $ PandocFileReadError $ "file not found: " ++ s + Left _ -> throwError $ PandocFileReadError s -- TODO: Make this more sensitive to the different sorts of failure readDataFile mfp fname = do eitherBS <- liftIO (tryIOError $ IO.readDataFile mfp fname) case eitherBS of Right bs -> return bs - Left _ -> throwError $ PandocFileReadError $ "file not found: " ++ fname + Left _ -> throwError $ PandocFileReadError fname fail = M.fail fetchItem ms s = liftIO $ IO.fetchItem ms s fetchItem' mb ms s = liftIO $ IO.fetchItem' mb ms s @@ -235,7 +239,7 @@ instance PandocMonad PandocPure where fps <- asks envFiles case lookup fp fps of Just bs -> return (BL.fromStrict bs) - Nothing -> throwError $ PandocFileReadError "file not in state" + Nothing -> throwError $ PandocFileReadError fp readDataFile Nothing "reference.docx" = do (B.concat . BL.toChunks . fromArchive) <$> (getDefaultReferenceDocx Nothing) readDataFile Nothing "reference.odt" = do @@ -253,7 +257,7 @@ instance PandocMonad PandocPure where fps <- asks envFiles case lookup fp fps of Just bs -> return (Right (bs, getMimeType fp)) - Nothing -> return (Left $ E.toException $ PandocFileReadError "oops") + Nothing -> return (Left $ E.toException $ PandocFileReadError fp) fetchItem' media sourceUrl nm = do case lookupMedia nm media of |