From: Karl Palsson <ka...@etactica.com>

Return an extra string to lua clients, not just the code.

Signed-off-by: Karl Palsson <ka...@etactica.com>
---

Makes it much more pleasant when working with ubus via lua, rather
than simply getting the integer return code. example code also
updated to demonstrate access to the code and message.

 lua/test_client.lua | 11 +++++++----
 lua/ubus.c          | 24 ++++++++++++++++--------
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/lua/test_client.lua b/lua/test_client.lua
index 0b60e0d..c006b4c 100755
--- a/lua/test_client.lua
+++ b/lua/test_client.lua
@@ -22,10 +22,13 @@ for i, n in ipairs(namespaces) do
        end
 end
 
-local status = conn:call("test", "hello", { msg = "eth0" })
-
-for k, v in pairs(status) do
-       print("key=" .. k .. " value=" .. tostring(v))
+local status, rc, desc = conn:call("test", "hello", { msg = "eth0" })
+if not status then
+       print("test.hello failed, code, desc: ", rc, desc)
+else
+       for k, v in pairs(status) do
+               print("key=" .. k .. " value=" .. tostring(v))
+       end
 end
 
 local status = {conn:call("test", "hello1", { msg = "eth0" })}
diff --git a/lua/ubus.c b/lua/ubus.c
index 86e34b7..0df2887 100644
--- a/lua/ubus.c
+++ b/lua/ubus.c
@@ -246,7 +246,8 @@ ubus_lua_connect(lua_State *L)
        /* NB: no errors from ubus_connect() yet */
        lua_pushnil(L);
        lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
-       return 2;
+       lua_pushstring(L, ubus_strerror(UBUS_STATUS_UNKNOWN_ERROR));
+       return 3;
 }
 
 
@@ -273,7 +274,8 @@ ubus_lua_objects(lua_State *L)
                lua_pop(L, 1);
                lua_pushnil(L);
                lua_pushinteger(L, rv);
-               return 2;
+               lua_pushstring(L, ubus_strerror(rv));
+               return 3;
        }
 
        return 1;
@@ -335,7 +337,8 @@ static int ubus_lua_reply(lua_State *L)
        {
                lua_pushnil(L);
                lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
-               return 2;
+               lua_pushstring(L, ubus_strerror(UBUS_STATUS_INVALID_ARGUMENT));
+               return 3;
        }
 
        req = lua_touserdata(L, 2);
@@ -529,7 +532,8 @@ ubus_lua_signatures(lua_State *L)
                lua_pop(L, 1);
                lua_pushnil(L);
                lua_pushinteger(L, rv);
-               return 2;
+               lua_pushstring(L, ubus_strerror(rv));
+               return 3;
        }
 
        return 1;
@@ -564,7 +568,8 @@ ubus_lua_call(lua_State *L)
        {
                lua_pushnil(L);
                lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
-               return 2;
+               lua_pushstring(L, ubus_strerror(UBUS_STATUS_INVALID_ARGUMENT));
+               return 3;
        }
 
        rv = ubus_lookup_id(c->ctx, path, &id);
@@ -573,7 +578,8 @@ ubus_lua_call(lua_State *L)
        {
                lua_pushnil(L);
                lua_pushinteger(L, rv);
-               return 2;
+               lua_pushstring(L, ubus_strerror(rv));
+               return 3;
        }
 
        top = lua_gettop(L);
@@ -584,7 +590,8 @@ ubus_lua_call(lua_State *L)
                lua_pop(L, 1);
                lua_pushnil(L);
                lua_pushinteger(L, rv);
-               return 2;
+               lua_pushstring(L, ubus_strerror(rv));
+               return 3;
        }
 
        return lua_gettop(L) - top;
@@ -669,7 +676,8 @@ ubus_lua_send(lua_State *L)
        if (!ubus_lua_format_blob_array(L, &c->buf, true)) {
                lua_pushnil(L);
                lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
-               return 2;
+               lua_pushstring(L, ubus_strerror(UBUS_STATUS_INVALID_ARGUMENT));
+               return 3;
        }
 
        // Send the event
-- 
2.4.11
_______________________________________________
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel

Reply via email to