git.iko.soy/typeable/pandoc
on master help sign in
Error: change type of handleError.

It now lives in IO and gives a proper message + exit instead of calling 'error'. We shouldn't be making it easier for people to raise error on pure code. And this is better for the main application in IO.

commitd7583f365951373158a55ce344ba6b345ea481ec authorJohn MacFarlane <jgm@berkeley.edu> dateSat 3 Dec 2016, 22:35 (10 years ago) parent1a0d93a
3 files changed+15−16
src/Text/Pandoc.hs+3−4 src/Text/Pandoc/Class.hs+1−1 src/Text/Pandoc/Error.hs+11−11
src/Text/Pandoc.hs+3−4
@@ -38,12 +38,11 @@ inline links:
3838> module Main where
3939> import Text.Pandoc
4040>
41−> markdownToRST :: String -> String
41+> markdownToRST :: String -> Either PandocError String
4242> markdownToRST =
43−> writeRST def {writerReferenceLinks = True} .
44−> handleError . readMarkdown def
43+> writeRST def {writerReferenceLinks = True} . readMarkdown def
4544>
46−> main = getContents >>= putStrLn . markdownToRST
45+> main = getContents >>= either error return markdownToRST >>= putStrLn
4746
4847Note: all of the readers assume that the input text has @'\n'@
4948line endings. So if you get your input text from a web form,
src/Text/Pandoc/Class.hs+1−1
@@ -190,7 +190,7 @@ withWarningsToStderr f = do
190190 return x
191191
192192runIOorExplode :: PandocIO a -> IO a
193−runIOorExplode ma = handleError <$> runIO ma
193+runIOorExplode ma = runIO ma >>= handleError
194194
195195newtype PandocIO a = PandocIO {
196196 unPandocIO :: ExceptT PandocError (StateT CommonState IO) a
src/Text/Pandoc/Error.hs+11−11
@@ -35,6 +35,7 @@ import Text.Parsec.Error
3535import Text.Parsec.Pos hiding (Line)
3636import Data.Generics (Typeable)
3737import Control.Exception (Exception)
38+import Text.Pandoc.Shared (err)
3839
3940type Input = String
4041
@@ -54,15 +55,15 @@ data PandocError = PandocFileReadError FilePath
5455
5556instance Exception PandocError
5657
57−-- | An unsafe method to handle `PandocError`s.
58−handleError :: Either PandocError a -> a
59−handleError (Right r) = r
60−handleError (Left err) =
61− case err of
62− PandocFileReadError fp -> error $ "problem reading " ++ fp
63− PandocShouldNeverHappenError s -> error s
64− PandocSomeError s -> error s
65− PandocParseError s -> error s
58+-- | Handle PandocError by exiting with an error message.
59+handleError :: Either PandocError a -> IO a
60+handleError (Right r) = return r
61+handleError (Left e) =
62+ case e of
63+ PandocFileReadError fp -> err 61 $ "problem reading " ++ fp
64+ PandocShouldNeverHappenError s -> err 62 s
65+ PandocSomeError s -> err 63 s
66+ PandocParseError s -> err 64 s
6667 PandocParsecError input err' ->
6768 let errPos = errorPos err'
6869 errLine = sourceLine errPos
@@ -73,6 +74,5 @@ handleError (Left err) =
7374 ,"\n", replicate (errColumn - 1) ' '
7475 ,"^"]
7576 else ""
76− in error $ "\nError at " ++ show err'
77− ++ errorInFile
77+ in err 65 $ "\nError at " ++ show err' ++ errorInFile
7878