From patchwork Fri Apr 29 13:53:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karl Palsson X-Patchwork-Id: 616792 X-Patchwork-Delegate: blogic@openwrt.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (caladan.dune.hu [78.24.191.180]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qxFYG1cWcz9sDX for ; Fri, 29 Apr 2016 23:54:10 +1000 (AEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 428EFB9158A; Fri, 29 Apr 2016 15:54:04 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.1 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP; Fri, 29 Apr 2016 15:54:04 +0200 (CEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id DBD72B91589 for ; Fri, 29 Apr 2016 15:54:02 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 CL_IP_EQ_FROM_IP=-2 (check from: .beeroclock. - helo: .palmtree.beeroclock. - helo-domain: .beeroclock.) FROM/MX_MATCHES_HELO(DOMAIN)=-2; rate: -7 Received: from palmtree.beeroclock.net (palmtree.beeroclock.net [178.79.160.154]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Fri, 29 Apr 2016 15:54:02 +0200 (CEST) Received: by palmtree.beeroclock.net (Postfix, from userid 1000) id B609F1099A; Fri, 29 Apr 2016 13:54:01 +0000 (UTC) From: Karl Palsson To: openwrt-devel@lists.openwrt.org Date: Fri, 29 Apr 2016 13:53:58 +0000 Message-Id: <1461938039-24143-1-git-send-email-karlp@tweak.net.au> X-Mailer: git-send-email 1.9.1 Subject: [OpenWrt-Devel] [PATCH 1/2] ubus: lua: return string errors, not just codes X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Karl Palsson MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" From: Karl Palsson Return an extra string to lua clients, not just the code. Signed-off-by: Karl Palsson --- 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