@@ -16,9 +16,10 @@ encountered during parsing.
1616-}
1717module Text.Pandoc.Error (
1818 PandocError(..),
19+ renderError,
1920 handleError) where
2021
21−import Control.Exception (Exception)
22+import Control.Exception (Exception, displayException)
2223import Data.Typeable (Typeable)
2324import Data.Word (Word8)
2425import Data.Text (Text)
@@ -68,19 +69,17 @@ data PandocError = PandocIOError Text IOError
6869
6970instance Exception PandocError
7071
71−-- | Handle PandocError by exiting with an error message.
72−handleError :: Either PandocError a -> IO a
73−handleError (Right r) = return r
74−handleError (Left e) =
72+renderError :: PandocError -> Text
73+renderError e =
7574 case e of
76− PandocIOError _ err' -> ioError err'
77− PandocHttpError u err' -> err 61 $
75+ PandocIOError _ err' -> T.pack $ displayException err'
76+ PandocHttpError u err' ->
7877 "Could not fetch " <> u <> "\n" <> tshow err'
79− PandocShouldNeverHappenError s -> err 62 $
78+ PandocShouldNeverHappenError s ->
8079 "Something we thought was impossible happened!\n" <>
8180 "Please report this to pandoc's developers: " <> s
82− PandocSomeError s -> err 63 s
83− PandocParseError s -> err 64 s
81+ PandocSomeError s -> s
82+ PandocParseError s -> s
8483 PandocParsecError input err' ->
8584 let errPos = errorPos err'
8685 errLine = sourceLine errPos
@@ -91,41 +90,41 @@ handleError (Left e) =
9190 ,"\n", T.replicate (errColumn - 1) " "
9291 ,"^"]
9392 else ""
94− in err 65 $ "\nError at " <> tshow err' <>
93+ in "\nError at " <> tshow err' <>
9594 -- if error comes from a chunk or included file,
9695 -- then we won't get the right text this way:
9796 if sourceName errPos == "source"
9897 then errorInFile
9998 else ""
100− PandocMakePDFError s -> err 66 s
101− PandocOptionError s -> err 6 s
102− PandocSyntaxMapError s -> err 67 s
103− PandocFailOnWarningError -> err 3 "Failing because there were warnings."
104− PandocPDFProgramNotFoundError pdfprog -> err 47 $
99+ PandocMakePDFError s -> s
100+ PandocOptionError s -> s
101+ PandocSyntaxMapError s -> s
102+ PandocFailOnWarningError -> "Failing because there were warnings."
103+ PandocPDFProgramNotFoundError pdfprog ->
105104 pdfprog <> " not found. Please select a different --pdf-engine or install " <> pdfprog
106− PandocPDFError logmsg -> err 43 $ "Error producing PDF.\n" <> logmsg
107− PandocXMLError fp logmsg -> err 44 $ "Invalid XML" <>
105+ PandocPDFError logmsg -> "Error producing PDF.\n" <> logmsg
106+ PandocXMLError fp logmsg -> "Invalid XML" <>
108107 (if T.null fp then "" else " in " <> fp) <> ":\n" <> logmsg
109− PandocFilterError filtername msg -> err 83 $ "Error running filter " <>
108+ PandocFilterError filtername msg -> "Error running filter " <>
110109 filtername <> ":\n" <> msg
111− PandocLuaError msg -> err 84 $ "Error running Lua:\n" <> msg
112− PandocCouldNotFindDataFileError fn -> err 97 $
110+ PandocLuaError msg -> "Error running Lua:\n" <> msg
111+ PandocCouldNotFindDataFileError fn ->
113112 "Could not find data file " <> fn
114− PandocResourceNotFound fn -> err 99 $
113+ PandocResourceNotFound fn ->
115114 "File " <> fn <> " not found in resource path"
116− PandocTemplateError s -> err 5 $ "Error compiling template " <> s
117− PandocAppError s -> err 4 s
118− PandocEpubSubdirectoryError s -> err 31 $
115+ PandocTemplateError s -> "Error compiling template " <> s
116+ PandocAppError s -> s
117+ PandocEpubSubdirectoryError s ->
119118 "EPUB subdirectory name '" <> s <> "' contains illegal characters"
120− PandocMacroLoop s -> err 91 $
119+ PandocMacroLoop s ->
121120 "Loop encountered in expanding macro " <> s
122− PandocUTF8DecodingError f offset w -> err 92 $
121+ PandocUTF8DecodingError f offset w ->
123122 "UTF-8 decoding error in " <> f <> " at byte offset " <> tshow offset <>
124123 " (" <> T.pack (printf "%2x" w) <> ").\n" <>
125124 "The input must be a UTF-8 encoded text."
126− PandocIpynbDecodingError w -> err 93 $
125+ PandocIpynbDecodingError w ->
127126 "ipynb decoding error: " <> w
128− PandocUnknownReaderError r -> err 21 $
127+ PandocUnknownReaderError r ->
129128 "Unknown input format " <> r <>
130129 case r of
131130 "doc" -> "\nPandoc can convert from DOCX, but not from DOC." <>
@@ -133,7 +132,7 @@ handleError (Left e) =
133132 " and convert that with pandoc."
134133 "pdf" -> "\nPandoc can convert to PDF, but not from PDF."
135134 _ -> ""
136− PandocUnknownWriterError w -> err 22 $
135+ PandocUnknownWriterError w ->
137136 "Unknown output format " <> w <>
138137 case w of
139138 "pdf" -> "To create a pdf using pandoc, use" <>
@@ -142,14 +141,54 @@ handleError (Left e) =
142141 ".pdf extension (-o filename.pdf)."
143142 "doc" -> "\nPandoc can convert to DOCX, but not to DOC."
144143 _ -> ""
145− PandocUnsupportedExtensionError ext f -> err 23 $
144+ PandocUnsupportedExtensionError ext f ->
146145 "The extension " <> ext <> " is not supported " <>
147146 "for " <> f
148− PandocCiteprocError e' -> err 24 $
147+ PandocCiteprocError e' ->
149148 prettyCiteprocError e'
150− PandocBibliographyError fp msg -> err 25 $
149+ PandocBibliographyError fp msg ->
151150 "Error reading bibliography file " <> fp <> ":\n" <> msg
152151
152+
153+-- | Handle PandocError by exiting with an error message.
154+handleError :: Either PandocError a -> IO a
155+handleError (Right r) = return r
156+handleError (Left e) =
157+ case e of
158+ PandocIOError _ err' -> ioError err'
159+ _ -> err exitCode (renderError e)
160+ where
161+ exitCode =
162+ case e of
163+ PandocIOError{} -> 1
164+ PandocHttpError{} -> 61
165+ PandocShouldNeverHappenError{} -> 62
166+ PandocSomeError{} -> 63
167+ PandocParseError{} -> 64
168+ PandocParsecError{} -> 65
169+ PandocMakePDFError{} -> 66
170+ PandocOptionError{} -> 6
171+ PandocSyntaxMapError{} -> 67
172+ PandocFailOnWarningError{} -> 3
173+ PandocPDFProgramNotFoundError{} -> 47
174+ PandocPDFError{} -> 43
175+ PandocXMLError{} -> 44
176+ PandocFilterError{} -> 83
177+ PandocLuaError{} -> 84
178+ PandocCouldNotFindDataFileError{} -> 97
179+ PandocResourceNotFound fn -> 99
180+ PandocTemplateError{} -> 5
181+ PandocAppError{} -> 4
182+ PandocEpubSubdirectoryError{} -> 31
183+ PandocMacroLoop s -> 91
184+ PandocUTF8DecodingError{} -> 92
185+ PandocIpynbDecodingError{} -> 93
186+ PandocUnknownReaderError{} -> 21
187+ PandocUnknownWriterError{} -> 22
188+ PandocUnsupportedExtensionError{} -> 23
189+ PandocCiteprocError{} -> 24
190+ PandocBibliographyError{} -> 25
191+
153192err :: Int -> Text -> IO a
154193err exitCode msg = do
155194 UTF8.hPutStrLn stderr (T.unpack msg)