wplua: add proper GError domain & error codes

This commit is contained in:
George Kiagiadakis 2020-12-15 18:36:14 +02:00
parent 8b4c5af49c
commit 65b817f6fc
3 changed files with 31 additions and 6 deletions

View file

@ -14,6 +14,8 @@
extern void _wplua_register_resource (void);
G_DEFINE_QUARK (wplua, wp_domain_lua);
static void
_wplua_openlibs (lua_State *L)
{
@ -163,7 +165,7 @@ _wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
ret = luaL_loadbuffer (L, buf, size, name);
if (ret != LUA_OK) {
g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_COMPILATION,
"Failed to compile: %s", lua_tostring (L, -1));
lua_pop (L, sandbox + 1);
return FALSE;
@ -171,7 +173,7 @@ _wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
ret = lua_pcall (L, sandbox, 0, 0);
if (ret != LUA_OK) {
g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME,
"Failed to run: %s", lua_tostring (L, -1));
lua_pop (L, 1);
return FALSE;

View file

@ -17,6 +17,29 @@
G_BEGIN_DECLS
/**
* WP_DOMAIN_LUA:
*
* A #GError domain for errors that occurred within the context of the
* WirePlumber lua library.
*/
#define WP_DOMAIN_LUA (wp_domain_lua_quark ())
GQuark wp_domain_lua_quark (void);
/**
* WpLuaError:
* @WP_LUA_ERROR_COMPILATION: a compilation error, i.e. invalid Lua code
* @WP_LUA_ERROR_RUNTIME: a runtime error, i.e. misbehaving Lua code
*
* Error codes that can appear in a #GError when the error domain
* is %WP_DOMAIN_LUA
*/
typedef enum {
WP_LUA_ERROR_COMPILATION,
WP_LUA_ERROR_RUNTIME,
} WpLuaError;
lua_State * wplua_new (void);
void wplua_free (lua_State * L);

View file

@ -426,7 +426,7 @@ test_wplua_sandbox ()
"o = TestObject.new()\n";
wplua_load_buffer (L, code2, sizeof (code2) - 1, &error);
g_debug ("expected error: %s", error ? error->message : "null");
g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED);
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME);
g_clear_error (&error);
const gchar code3[] =
@ -443,21 +443,21 @@ test_wplua_sandbox ()
"o:call('change', 'by Lua', 55)\n";
wplua_load_buffer (L, code5, sizeof (code5) - 1, &error);
g_debug ("expected error: %s", error ? error->message : "null");
g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED);
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME);
g_clear_error (&error);
const gchar code6[] =
"string.test = 'hello world'\n";
wplua_load_buffer (L, code6, sizeof (code6) - 1, &error);
g_debug ("expected error: %s", error ? error->message : "null");
g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED);
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME);
g_clear_error (&error);
const gchar code7[] =
"Table.test = 'hello world'\n";
wplua_load_buffer (L, code7, sizeof (code7) - 1, &error);
g_debug ("expected error: %s", error ? error->message : "null");
g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED);
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME);
g_clear_error (&error);
wplua_free (L);