@@ -16,9 +16,10 @@ encountered during parsing.
17module Text.Pandoc.Error (
17module Text.Pandoc.Error (
18 PandocError(..),
18 PandocError(..),
19 handleError) where
20 handleError) where
21−import Control.Exception (Exception)
22+import Control.Exception (Exception, displayException)
22import Data.Typeable (Typeable)
23import Data.Typeable (Typeable)
23import Data.Word (Word8)
24import Data.Word (Word8)
24import Data.Text (Text)
25import Data.Text (Text)
@@ -68,19 +69,17 @@ data PandocError = PandocIOError Text IOError
69instance Exception PandocError
70instance Exception PandocError
71−-- | Handle PandocError by exiting with an error message.
72+renderError :: PandocError -> Text
72−handleError :: Either PandocError a -> IO a
73+renderError e =
73−handleError (Right r) = return r
74−handleError (Left e) =
76− PandocIOError _ err' -> ioError err'
75+ PandocIOError _ err' -> T.pack $ displayException err'
77− PandocHttpError u err' -> err 61 $
76+ PandocHttpError u err' ->
78 "Could not fetch " <> u <> "\n" <> tshow err'
77 "Could not fetch " <> u <> "\n" <> tshow err'
79− PandocShouldNeverHappenError s -> err 62 $
78+ PandocShouldNeverHappenError s ->
80 "Something we thought was impossible happened!\n" <>
79 "Something we thought was impossible happened!\n" <>
81 "Please report this to pandoc's developers: " <> s
80 "Please report this to pandoc's developers: " <> s
82− PandocSomeError s -> err 63 s
81+ PandocSomeError s -> s
83− PandocParseError s -> err 64 s
82+ PandocParseError s -> s
84 PandocParsecError input err' ->
83 PandocParsecError input err' ->
85 let errPos = errorPos err'
84 let errPos = errorPos err'
86 errLine = sourceLine errPos
85 errLine = sourceLine errPos
@@ -91,41 +90,41 @@ handleError (Left e) =
91 ,"\n", T.replicate (errColumn - 1) " "
90 ,"\n", T.replicate (errColumn - 1) " "
94− in err 65 $ "\nError at " <> tshow err' <>
93+ in "\nError at " <> tshow err' <>
95 -- if error comes from a chunk or included file,
94 -- if error comes from a chunk or included file,
96 -- then we won't get the right text this way:
95 -- then we won't get the right text this way:
97 if sourceName errPos == "source"
96 if sourceName errPos == "source"
98 then errorInFile
97 then errorInFile
100− PandocMakePDFError s -> err 66 s
99+ PandocMakePDFError s -> s
101− PandocOptionError s -> err 6 s
100+ PandocOptionError s -> s
102− PandocSyntaxMapError s -> err 67 s
101+ PandocSyntaxMapError s -> s
103− PandocFailOnWarningError -> err 3 "Failing because there were warnings."
102+ PandocFailOnWarningError -> "Failing because there were warnings."
104− PandocPDFProgramNotFoundError pdfprog -> err 47 $
103+ PandocPDFProgramNotFoundError pdfprog ->
105 pdfprog <> " not found. Please select a different --pdf-engine or install " <> pdfprog
104 pdfprog <> " not found. Please select a different --pdf-engine or install " <> pdfprog
106− PandocPDFError logmsg -> err 43 $ "Error producing PDF.\n" <> logmsg
105+ PandocPDFError logmsg -> "Error producing PDF.\n" <> logmsg
107− PandocXMLError fp logmsg -> err 44 $ "Invalid XML" <>
106+ PandocXMLError fp logmsg -> "Invalid XML" <>
108 (if T.null fp then "" else " in " <> fp) <> ":\n" <> logmsg
107 (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 " <>
110 filtername <> ":\n" <> msg
109 filtername <> ":\n" <> msg
111− PandocLuaError msg -> err 84 $ "Error running Lua:\n" <> msg
110+ PandocLuaError msg -> "Error running Lua:\n" <> msg
112− PandocCouldNotFindDataFileError fn -> err 97 $
111+ PandocCouldNotFindDataFileError fn ->
113 "Could not find data file " <> fn
112 "Could not find data file " <> fn
114− PandocResourceNotFound fn -> err 99 $
113+ PandocResourceNotFound fn ->
115 "File " <> fn <> " not found in resource path"
114 "File " <> fn <> " not found in resource path"
116− PandocTemplateError s -> err 5 $ "Error compiling template " <> s
115+ PandocTemplateError s -> "Error compiling template " <> s
117− PandocAppError s -> err 4 s
116+ PandocAppError s -> s
118− PandocEpubSubdirectoryError s -> err 31 $
117+ PandocEpubSubdirectoryError s ->
119 "EPUB subdirectory name '" <> s <> "' contains illegal characters"
118 "EPUB subdirectory name '" <> s <> "' contains illegal characters"
120− PandocMacroLoop s -> err 91 $
119+ PandocMacroLoop s ->
121 "Loop encountered in expanding macro " <> s
120 "Loop encountered in expanding macro " <> s
122− PandocUTF8DecodingError f offset w -> err 92 $
121+ PandocUTF8DecodingError f offset w ->
123 "UTF-8 decoding error in " <> f <> " at byte offset " <> tshow offset <>
122 "UTF-8 decoding error in " <> f <> " at byte offset " <> tshow offset <>
124 " (" <> T.pack (printf "%2x" w) <> ").\n" <>
123 " (" <> T.pack (printf "%2x" w) <> ").\n" <>
125 "The input must be a UTF-8 encoded text."
124 "The input must be a UTF-8 encoded text."
126− PandocIpynbDecodingError w -> err 93 $
125+ PandocIpynbDecodingError w ->
127 "ipynb decoding error: " <> w
126 "ipynb decoding error: " <> w
128− PandocUnknownReaderError r -> err 21 $
127+ PandocUnknownReaderError r ->
129 "Unknown input format " <> r <>
128 "Unknown input format " <> r <>
130 case r of
129 case r of
131 "doc" -> "\nPandoc can convert from DOCX, but not from DOC." <>
130 "doc" -> "\nPandoc can convert from DOCX, but not from DOC." <>
@@ -133,7 +132,7 @@ handleError (Left e) =
133 " and convert that with pandoc."
132 " and convert that with pandoc."
134 "pdf" -> "\nPandoc can convert to PDF, but not from PDF."
133 "pdf" -> "\nPandoc can convert to PDF, but not from PDF."
136− PandocUnknownWriterError w -> err 22 $
135+ PandocUnknownWriterError w ->
137 "Unknown output format " <> w <>
136 "Unknown output format " <> w <>
138 case w of
137 case w of
139 "pdf" -> "To create a pdf using pandoc, use" <>
138 "pdf" -> "To create a pdf using pandoc, use" <>
@@ -142,14 +141,54 @@ handleError (Left e) =
142 ".pdf extension (-o filename.pdf)."
141 ".pdf extension (-o filename.pdf)."
143 "doc" -> "\nPandoc can convert to DOCX, but not to DOC."
142 "doc" -> "\nPandoc can convert to DOCX, but not to DOC."
145− PandocUnsupportedExtensionError ext f -> err 23 $
144+ PandocUnsupportedExtensionError ext f ->
146 "The extension " <> ext <> " is not supported " <>
145 "The extension " <> ext <> " is not supported " <>
147 "for " <> f
146 "for " <> f
148− PandocCiteprocError e' -> err 24 $
147+ PandocCiteprocError e' ->
149 prettyCiteprocError e'
148 prettyCiteprocError e'
150− PandocBibliographyError fp msg -> err 25 $
149+ PandocBibliographyError fp msg ->
151 "Error reading bibliography file " <> fp <> ":\n" <> msg
150 "Error reading bibliography file " <> fp <> ":\n" <> msg
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) =
158+ PandocIOError _ err' -> ioError err'
159+ _ -> err exitCode (renderError e)
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
153err :: Int -> Text -> IO a
192err :: Int -> Text -> IO a
154err exitCode msg = do
193err exitCode msg = do
155 UTF8.hPutStrLn stderr (T.unpack msg)
194 UTF8.hPutStrLn stderr (T.unpack msg)