summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Lua.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-06-20 16:09:33 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2017-06-20 16:19:59 +0200
commitb78afbd9803c75fcf2db32b4ce4ded1b8fa0224a (patch)
tree93a1542b71c908f3995ccc0debaebaa38b800446 /src/Text/Pandoc/Lua.hs
parent21925284244bb88f927c287c21b48df35234b260 (diff)
Text.Pandoc.Lua: throw LuaException instead of using 'error'.
Text.Pandoc.App: trap LuaException and issue a PandocFilterError.
Diffstat (limited to 'src/Text/Pandoc/Lua.hs')
-rw-r--r--src/Text/Pandoc/Lua.hs21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index f74c0e425..e9184c7ce 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -28,11 +28,15 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Pandoc lua utils.
-}
-module Text.Pandoc.Lua ( runLuaFilter, pushPandocModule ) where
+module Text.Pandoc.Lua ( LuaException(..),
+ runLuaFilter,
+ pushPandocModule ) where
+import Control.Exception
import Control.Monad (unless, when, (>=>))
import Control.Monad.Trans (MonadIO (..))
import Data.Map (Map)
+import Data.Typeable (Typeable)
import Scripting.Lua (LuaState, StackValue (..))
import Text.Pandoc.Definition
import Text.Pandoc.Lua.PandocModule (pushPandocModule)
@@ -42,6 +46,11 @@ import Text.Pandoc.Walk
import qualified Data.Map as Map
import qualified Scripting.Lua as Lua
+data LuaException = LuaException String
+ deriving (Show, Typeable)
+
+instance Exception LuaException
+
runLuaFilter :: (MonadIO m)
=> FilePath -> [String] -> Pandoc -> m Pandoc
runLuaFilter filterPath args pd = liftIO $ do
@@ -59,7 +68,7 @@ runLuaFilter filterPath args pd = liftIO $ do
if (status /= 0)
then do
Just luaErrMsg <- Lua.peek lua 1
- error luaErrMsg
+ throwIO (LuaException luaErrMsg)
else do
Lua.call lua 0 Lua.multret
newtop <- Lua.gettop lua
@@ -195,8 +204,9 @@ instance StackValue a => PushViaFilterFunction (IO a) where
Lua.call lua num 1
mbres <- Lua.peek lua (-1)
case mbres of
- Nothing -> error $ "Error while trying to get a filter's return "
- ++ "value from lua stack."
+ Nothing -> throwIO $ LuaException
+ ("Error while trying to get a filter's return "
+ ++ "value from lua stack.")
Just res -> res <$ Lua.pop lua 1
instance (StackValue a, PushViaFilterFunction b) =>
@@ -225,7 +235,8 @@ instance StackValue LuaFilterFunction where
push lua v = pushFilterFunction lua v
peek lua i = do
isFn <- Lua.isfunction lua i
- unless isFn (error $ "Not a function at index " ++ (show i))
+ unless isFn (throwIO $ LuaException $
+ "Not a function at index " ++ (show i))
Lua.pushvalue lua i
push lua ("PANDOC_FILTER_FUNCTIONS"::String)
Lua.rawget lua Lua.registryindex