@@ -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