aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert.krewinkel@tourstream.eu>2017-06-27 17:55:47 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2017-06-27 17:55:47 +0200
commitf5f84859230568ddafb2e7e23b5d9b3e98fdbba5 (patch)
tree8c512c48e0c4de27697a25146f05267224a39072 /src/Text
parent4282abbd0781cf5e6731a9b43dc8cfeb1dca58fa (diff)
downloadpandoc-f5f84859230568ddafb2e7e23b5d9b3e98fdbba5.tar.gz
Text.Pandoc.Lua: catch lua errors in filter functions
Replace lua errors with `LuaException`s.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Lua.hs31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index 3770880f3..2ee8d0847 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -196,17 +196,26 @@ runFilterFunction :: StackValue a => LuaState -> LuaFilterFunction -> a -> IO a
runFilterFunction lua lf x = do
pushFilterFunction lua lf
Lua.push lua x
- Lua.call lua 1 1
- resType <- Lua.ltype lua (-1)
- case resType of
- Lua.TNIL -> Lua.pop lua 1 *> return x
- _ -> do
- mbres <- Lua.peek lua (-1)
- case mbres of
- Nothing -> throwIO $ LuaException
- ("Error while trying to get a filter's return "
- ++ "value from lua stack.")
- Just res -> res <$ Lua.pop lua 1
+ z <- Lua.pcall lua 1 1 0
+ if (z /= 0)
+ then do
+ msg <- Lua.peek lua (-1)
+ let prefix = "Error while running filter function: "
+ throwIO . LuaException $
+ case msg of
+ Nothing -> prefix ++ "could not read error message"
+ Just msg' -> prefix ++ msg'
+ else do
+ resType <- Lua.ltype lua (-1)
+ case resType of
+ Lua.TNIL -> Lua.pop lua 1 *> return x
+ _ -> do
+ mbres <- Lua.peek lua (-1)
+ case mbres of
+ Nothing -> throwIO $ LuaException
+ ("Error while trying to get a filter's return "
+ ++ "value from lua stack.")
+ Just res -> res <$ Lua.pop lua 1
-- | Push the filter function to the top of the stack.
pushFilterFunction :: Lua.LuaState -> LuaFilterFunction -> IO ()