hslua-core-2.3.2: Bindings to Lua, an embeddable scripting language

Copyright | © 2017-2024 Albert Krewinkel
License | MIT
Maintainer | Albert Krewinkel <tarleb@hslua.org>
Safe Haskell | None
Language | Haskell2010

HsLua.Core.Error

Contents

- Helpers for hslua C wrapper functions.
- Orphan instances

Description

Lua exceptions and exception handling.

Synopsis

- newtype Exception = Exception {
  - exceptionMessage :: String
  }
- class Exception e => LuaError e where
  - popException :: LuaE e e
  - pushException :: e -> LuaE e ()
  - luaException :: String -> e
- type Lua a = LuaE Exception a
- try :: Exception e => LuaE e a -> LuaE e (Either e a)
- failLua :: LuaError e => String -> LuaE e a
- throwErrorAsException :: LuaError e => LuaE e a
- throwTypeMismatchError :: LuaError e => ByteString -> StackIndex -> LuaE e a
- changeErrorType :: forall old new a. LuaE old a -> LuaE new a
- liftLuaThrow :: LuaError e => (State -> Ptr StatusCode -> IO a) -> LuaE e a
- popErrorMessage :: State -> IO ByteString
- pushTypeMismatchError :: ByteString -> StackIndex -> LuaE e ()

# Documentation

newtype Exception Source #

Default Lua error type. Exceptions raised by Lua-related operations.

Constructors

Exception |

Fields

- exceptionMessage :: String

#### Instances

Instances details

Exception Exception Source # |
Instance details Defined in HsLua.Core.Error Methods toException :: Exception -> SomeException Source # fromException :: SomeException -> Maybe Exception Source # displayException :: Exception -> String Source # backtraceDesired :: Exception -> Bool Source #
Show Exception Source # |
Instance details Defined in HsLua.Core.Error Methods showsPrec :: Int -> Exception -> ShowS Source # show :: Exception -> String Source # showList :: [Exception] -> ShowS Source #
Eq Exception Source # |
Instance details Defined in HsLua.Core.Error Methods (==) :: Exception -> Exception -> Bool Source # (/=) :: Exception -> Exception -> Bool Source #
LuaError Exception Source # |
Instance details Defined in HsLua.Core.Error Methods popException :: LuaE Exception Exception Source # pushException :: Exception -> LuaE Exception () Source # luaException :: String -> Exception Source #

class Exception e => LuaError e where Source #

Any type that you wish to use for error handling in HsLua must be an instance of the `LuaError` class.

Methods

popException :: LuaE e e Source #

Converts the error at the top of the stack into an exception and pops the error off the stack.

This function is expected to produce a valid result for any Lua value; neither a Haskell exception nor a Lua error may result when this is called.

pushException :: e -> LuaE e () Source #

Pushes an exception to the top of the Lua stack. The pushed Lua object is used as an error object, and it is recommended that calling `tostring()` on the object produces an informative message.

luaException :: String -> e Source #

Creates a new exception with the given message.

#### Instances

Instances details

LuaError Exception Source # |
Instance details Defined in HsLua.Core.Error Methods popException :: LuaE Exception Exception Source # pushException :: Exception -> LuaE Exception () Source # luaException :: String -> Exception Source #

type Lua a = LuaE Exception a Source #

A Lua operation.

This type is suitable for most users. It uses a default exception for error handling. Users who need more control over error handling can use `LuaE` with a custom error type instead.

try :: Exception e => LuaE e a -> LuaE e (Either e a) Source #

Return either the result of a Lua computation or, if an exception was thrown, the error.

failLua :: LuaError e => String -> LuaE e a Source #

Raises an exception in the Lua monad.

throwErrorAsException :: LuaError e => LuaE e a Source #

Converts a Lua error at the top of the stack into a Haskell exception and throws it.

throwTypeMismatchError :: LuaError e => ByteString -> StackIndex -> LuaE e a Source #

Raises an exception that's appropriate when the type of a Lua object at the given index did not match the expected type. The name or description of the expected type is taken as an argument.

changeErrorType :: forall old new a. LuaE old a -> LuaE new a Source #

Change the error type of a computation.

# Helpers for hslua C wrapper functions.

liftLuaThrow :: LuaError e => (State -> Ptr StatusCode -> IO a) -> LuaE e a Source #

Takes a failable HsLua function and transforms it into a monadic `LuaE` operation. Throws an exception if an error occured.

popErrorMessage :: State -> IO ByteString Source #

Retrieve and pop the top object as an error message. This is very similar to tostring', but ensures that we don't recurse if getting the message failed.

This helpful as a "last resort" method when implementing `popException`.

pushTypeMismatchError Source #

Arguments

:: ByteString | name or description of expected type
-> StackIndex | stack index of mismatching object
-> LuaE e () |

Creates an error to notify about a Lua type mismatch and pushes it to the stack.

# Orphan instances

LuaError e => Alternative (LuaE e) Source # |
Instance details Methods empty :: LuaE e a Source # (<|>) :: LuaE e a -> LuaE e a -> LuaE e a Source # some :: LuaE e a -> LuaE e [a] Source # many :: LuaE e a -> LuaE e [a] Source #
LuaError e => MonadFail (LuaE e) Source # |
Instance details Methods fail :: String -> LuaE e a Source #