From 3d93414e5d433cad21ae2cbf45658d5789571f57 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 26 Oct 2020 14:46:53 -0700 Subject: Add PandocBibliographyError and use it in parsing bibliographies. This ensures that bibliography parsing errors generate messages that include the bibliography file name -- otherwise it can be quite mysterious where it is coming from. [API change] New PandocBibliographyError constructor on PandocError type. --- src/Text/Pandoc/Citeproc.hs | 12 +++++++----- src/Text/Pandoc/Error.hs | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/Text/Pandoc') diff --git a/src/Text/Pandoc/Citeproc.hs b/src/Text/Pandoc/Citeproc.hs index ec7954d47..175fbadb2 100644 --- a/src/Text/Pandoc/Citeproc.hs +++ b/src/Text/Pandoc/Citeproc.hs @@ -28,7 +28,7 @@ import Text.Pandoc.Builder as B import Text.Pandoc (PandocMonad(..), PandocError(..), readDataFile, ReaderOptions(..), pandocExtensions, report, LogMessage(..), fetchItem) -import Text.Pandoc.Shared (stringify, ordNub, blocksToInlines) +import Text.Pandoc.Shared (stringify, ordNub, blocksToInlines, tshow) import qualified Text.Pandoc.UTF8 as UTF8 import Data.Aeson (eitherDecode) import Data.Default @@ -206,16 +206,18 @@ getRefs :: PandocMonad m -> Maybe FilePath -> ByteString -> m [Reference Inlines] -getRefs locale format idpred mbfp raw = +getRefs locale format idpred mbfp raw = do + let err' = throwError . + PandocBibliographyError (maybe mempty T.pack mbfp) case format of Format_bibtex -> - either (throwError . PandocAppError . T.pack . show) return . + either (err' . tshow) return . readBibtexString Bibtex locale idpred . UTF8.toText $ raw Format_biblatex -> - either (throwError . PandocAppError . T.pack . show) return . + either (err' . tshow) return . readBibtexString Biblatex locale idpred . UTF8.toText $ raw Format_json -> - either (throwError . PandocAppError . T.pack) + either (err' . T.pack) (return . filter (idpred . unItemId . referenceId)) . cslJsonToReferences $ raw Format_yaml -> do diff --git a/src/Text/Pandoc/Error.hs b/src/Text/Pandoc/Error.hs index bd24fec6f..93028ccca 100644 --- a/src/Text/Pandoc/Error.hs +++ b/src/Text/Pandoc/Error.hs @@ -62,6 +62,7 @@ data PandocError = PandocIOError Text IOError | PandocUnknownWriterError Text | PandocUnsupportedExtensionError Text Text | PandocCiteprocError CiteprocError + | PandocBibliographyError Text Text deriving (Show, Typeable, Generic) instance Exception PandocError @@ -143,6 +144,8 @@ handleError (Left e) = "for " <> f PandocCiteprocError e' -> err 24 $ prettyCiteprocError e' + PandocBibliographyError fp msg -> err 25 $ + "Error reading bibliography file " <> fp <> ":\n" <> msg err :: Int -> Text -> IO a err exitCode msg = do -- cgit v1.2.3