{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Text.Pandoc.Lua.ErrorConversion
( errorConversion
) where
import Foreign.Lua (Lua (..), NumResults)
import Text.Pandoc.Error (PandocError (PandocLuaError))
import Text.Pandoc.Lua.Marshaling.PandocError (pushPandocError, peekPandocError)
import qualified Control.Monad.Catch as Catch
import qualified Data.Text as T
import qualified Foreign.Lua as Lua
errorConversion :: Lua.ErrorConversion
errorConversion :: ErrorConversion
errorConversion = ErrorConversion :: (forall a. State -> IO a)
-> (forall a. String -> Lua a -> Lua a)
-> (forall a. Lua a -> Lua a -> Lua a)
-> (Lua NumResults -> Lua NumResults)
-> ErrorConversion
Lua.ErrorConversion
{ addContextToException :: forall a. String -> Lua a -> Lua a
Lua.addContextToException = forall a. String -> Lua a -> Lua a
addContextToException
, alternative :: forall a. Lua a -> Lua a -> Lua a
Lua.alternative = forall a. Lua a -> Lua a -> Lua a
alternative
, errorToException :: forall a. State -> IO a
Lua.errorToException = forall a. State -> IO a
errorToException
, exceptionToError :: Lua NumResults -> Lua NumResults
Lua.exceptionToError = Lua NumResults -> Lua NumResults
exceptionToError
}
errorToException :: forall a . Lua.State -> IO a
errorToException :: State -> IO a
errorToException State
l = State -> Lua a -> IO a
forall a. State -> Lua a -> IO a
Lua.unsafeRunWith State
l (Lua a -> IO a) -> Lua a -> IO a
forall a b. (a -> b) -> a -> b
$ do
PandocError
err <- StackIndex -> Lua PandocError
peekPandocError StackIndex
Lua.stackTop
StackIndex -> Lua ()
Lua.pop StackIndex
1
PandocError -> Lua a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
Catch.throwM PandocError
err
alternative :: forall a . Lua a -> Lua a -> Lua a
alternative :: Lua a -> Lua a -> Lua a
alternative Lua a
x Lua a
y = Lua a -> Lua (Either PandocError a)
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
Catch.try Lua a
x Lua (Either PandocError a)
-> (Either PandocError a -> Lua a) -> Lua a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left (PandocError
_ :: PandocError) -> Lua a
y
Right a
x' -> a -> Lua a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x'
addContextToException :: forall a . String -> Lua a -> Lua a
addContextToException :: String -> Lua a -> Lua a
addContextToException String
ctx Lua a
op = Lua a
op Lua a -> (PandocError -> Lua a) -> Lua a
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
`Catch.catch` \case
PandocLuaError Text
msg -> PandocError -> Lua a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
Catch.throwM (PandocError -> Lua a) -> PandocError -> Lua a
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocLuaError (String -> Text
T.pack String
ctx Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
msg)
PandocError
e -> PandocError -> Lua a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
Catch.throwM PandocError
e
exceptionToError :: Lua NumResults -> Lua NumResults
exceptionToError :: Lua NumResults -> Lua NumResults
exceptionToError Lua NumResults
op = Lua NumResults
op Lua NumResults -> (PandocError -> Lua NumResults) -> Lua NumResults
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
`Catch.catch` \PandocError
e -> do
PandocError -> Lua ()
pushPandocError PandocError
e
Lua NumResults
Lua.error