git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.Lua: throw LuaException instead of using 'error'.

Text.Pandoc.App: trap LuaException and issue a PandocFilterError.

commitb78afbd9803c75fcf2db32b4ce4ded1b8fa0224a authorJohn MacFarlane <jgm@berkeley.edu> dateTue 20 Jun 2017, 16:09 (9 years ago) parent2192528
2 files changed+25−8
src/Text/Pandoc/App.hs+9−3 src/Text/Pandoc/Lua.hs+16−5
src/Text/Pandoc/App.hs+9−3
@@ -78,7 +78,7 @@ import Text.Pandoc.Builder (setMeta)
7878import Text.Pandoc.Class (PandocIO, extractMedia, fillMediaBag, getLog,
7979 setResourcePath, withMediaBag, setTrace)
8080import Text.Pandoc.Highlighting (highlightingStyles)
81−import Text.Pandoc.Lua (runLuaFilter)
81+import Text.Pandoc.Lua (runLuaFilter, LuaException(..))
8282import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
8383import Text.Pandoc.PDF (makePDF)
8484import Text.Pandoc.Process (pipeProcess)
@@ -782,10 +782,16 @@ expandFilterPath mbDatadir fp = liftIO $ do
782782 _ -> return fp
783783
784784applyLuaFilters :: MonadIO m
785− => Maybe FilePath -> [FilePath] -> [String] -> Pandoc -> m Pandoc
785+ => Maybe FilePath -> [FilePath] -> [String] -> Pandoc
786+ -> m Pandoc
786787applyLuaFilters mbDatadir filters args d = do
787788 expandedFilters <- mapM (expandFilterPath mbDatadir) filters
788− foldrM ($) d $ map (flip runLuaFilter args) expandedFilters
789+ let go f d' = liftIO $ do
790+ res <- E.try (runLuaFilter f args d')
791+ case res of
792+ Right x -> return x
793+ Left (LuaException s) -> E.throw (PandocFilterError f s)
794+ foldrM ($) d $ map go expandedFilters
789795
790796applyFilters :: MonadIO m
791797 => Maybe FilePath -> [FilePath] -> [String] -> Pandoc -> m Pandoc
src/Text/Pandoc/Lua.hs+16−5
@@ -28,11 +28,15 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2828
2929Pandoc lua utils.
3030-}
31−module Text.Pandoc.Lua ( runLuaFilter, pushPandocModule ) where
31+module Text.Pandoc.Lua ( LuaException(..),
32+ runLuaFilter,
33+ pushPandocModule ) where
3234
35+import Control.Exception
3336import Control.Monad (unless, when, (>=>))
3437import Control.Monad.Trans (MonadIO (..))
3538import Data.Map (Map)
39+import Data.Typeable (Typeable)
3640import Scripting.Lua (LuaState, StackValue (..))
3741import Text.Pandoc.Definition
3842import Text.Pandoc.Lua.PandocModule (pushPandocModule)
@@ -42,6 +46,11 @@ import Text.Pandoc.Walk
4246import qualified Data.Map as Map
4347import qualified Scripting.Lua as Lua
4448
49+data LuaException = LuaException String
50+ deriving (Show, Typeable)
51+
52+instance Exception LuaException
53+
4554runLuaFilter :: (MonadIO m)
4655 => FilePath -> [String] -> Pandoc -> m Pandoc
4756runLuaFilter filterPath args pd = liftIO $ do
@@ -59,7 +68,7 @@ runLuaFilter filterPath args pd = liftIO $ do
5968 if (status /= 0)
6069 then do
6170 Just luaErrMsg <- Lua.peek lua 1
62− error luaErrMsg
71+ throwIO (LuaException luaErrMsg)
6372 else do
6473 Lua.call lua 0 Lua.multret
6574 newtop <- Lua.gettop lua
@@ -195,8 +204,9 @@ instance StackValue a => PushViaFilterFunction (IO a) where
195204 Lua.call lua num 1
196205 mbres <- Lua.peek lua (-1)
197206 case mbres of
198− Nothing -> error $ "Error while trying to get a filter's return "
199− ++ "value from lua stack."
207+ Nothing -> throwIO $ LuaException
208+ ("Error while trying to get a filter's return "
209+ ++ "value from lua stack.")
200210 Just res -> res <$ Lua.pop lua 1
201211
202212instance (StackValue a, PushViaFilterFunction b) =>
@@ -225,7 +235,8 @@ instance StackValue LuaFilterFunction where
225235 push lua v = pushFilterFunction lua v
226236 peek lua i = do
227237 isFn <- Lua.isfunction lua i
228− unless isFn (error $ "Not a function at index " ++ (show i))
238+ unless isFn (throwIO $ LuaException $
239+ "Not a function at index " ++ (show i))
229240 Lua.pushvalue lua i
230241 push lua ("PANDOC_FILTER_FUNCTIONS"::String)
231242 Lua.rawget lua Lua.registryindex