From 65b817f6fc0f1222036b9995f1e9dba9ae547d43 Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Tue, 15 Dec 2020 18:36:14 +0200 Subject: [PATCH] wplua: add proper GError domain & error codes --- lib/wplua/wplua.c | 6 ++++-- lib/wplua/wplua.h | 23 +++++++++++++++++++++++ tests/wplua/wplua.c | 8 ++++---- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/wplua/wplua.c b/lib/wplua/wplua.c index 4b82898b..dd41cea4 100644 --- a/lib/wplua/wplua.c +++ b/lib/wplua/wplua.c @@ -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; diff --git a/lib/wplua/wplua.h b/lib/wplua/wplua.h index 3c9ac013..9703d8f2 100644 --- a/lib/wplua/wplua.h +++ b/lib/wplua/wplua.h @@ -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); diff --git a/tests/wplua/wplua.c b/tests/wplua/wplua.c index 52836ed6..4c985279 100644 --- a/tests/wplua/wplua.c +++ b/tests/wplua/wplua.c @@ -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);