Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: vdavydov.dev@gmail.com
Subject: [PATCH 1/8] lua: fix box.error.raise
Date: Mon, 16 Apr 2018 21:39:11 +0300	[thread overview]
Message-ID: <860da7f4e8760673d6052614a5df1d4bdb67c06b.1523903144.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1523903144.git.v.shpilevoy@tarantool.org>
In-Reply-To: <cover.1523903144.git.v.shpilevoy@tarantool.org>

It did not work because raise is implemented as __index metatable
member, and error() is __call metatable member. The second one
takes additional implicit argument - self.
---
 src/box/lua/error.cc   | 78 +++++++++++++++++++++++++++++++++-----------------
 test/box/misc.result   | 39 +++++++++++++++++++++++++
 test/box/misc.test.lua | 18 ++++++++++++
 3 files changed, 108 insertions(+), 27 deletions(-)

diff --git a/src/box/lua/error.cc b/src/box/lua/error.cc
index 314907421..56cc2c563 100644
--- a/src/box/lua/error.cc
+++ b/src/box/lua/error.cc
@@ -42,25 +42,18 @@ extern "C" {
 #include "lua/utils.h"
 #include "box/error.h"
 
-static int
-luaT_error_raise(lua_State *L)
+static void
+luaT_error_create(lua_State *L, int top, int top_base)
 {
 	uint32_t code = 0;
 	const char *reason = NULL;
 	const char *file = "";
 	unsigned line = 0;
 	lua_Debug info;
-	/* lua_type(L, 1) == LUA_TTABLE - box.error table */
-	int top = lua_gettop(L);
-	if (top <= 1) {
-		/* re-throw saved exceptions (if any) */
-		if (box_error_last())
-			luaT_error(L);
-		return 0;
-	} else if (top >= 2 && lua_type(L, 2) == LUA_TNUMBER) {
-		code = lua_tonumber(L, 2);
+	if (top >= top_base && lua_type(L, top_base) == LUA_TNUMBER) {
+		code = lua_tonumber(L, top_base);
 		reason = tnt_errcode_desc(code);
-		if (top > 2) {
+		if (top > top_base) {
 			/* Call string.format(reason, ...) to format message */
 			lua_getglobal(L, "string");
 			if (lua_isnil(L, -1))
@@ -69,24 +62,29 @@ luaT_error_raise(lua_State *L)
 			if (lua_isnil(L, -1))
 				goto raise;
 			lua_pushstring(L, reason);
-			for (int i = 3; i <= top; i++)
+			for (int i = top_base + 1; i <= top; i++)
 				lua_pushvalue(L, i);
-			lua_call(L, top - 1, 1);
+			lua_call(L, top - top_base + 1, 1);
 			reason = lua_tostring(L, -1);
 		} else if (strchr(reason, '%') != NULL) {
 			/* Missing arguments to format string */
 			luaL_error(L, "box.error(): bad arguments");
 		}
-	} else if (top == 2 && lua_istable(L, 2)) {
-		/* A special case that rethrows raw error (used by net.box) */
-		lua_getfield(L, 2, "code");
-		code = lua_tonumber(L, -1);
-		lua_pop(L, 1);
-		lua_getfield(L, 2, "reason");
-		reason = lua_tostring(L, -1);
-		if (reason == NULL)
-			reason = "";
-		lua_pop(L, 1);
+	} else if (top == top_base) {
+		if (lua_istable(L, top_base)) {
+			/* A special case that rethrows raw error (used by net.box) */
+			lua_getfield(L, top_base, "code");
+			code = lua_tonumber(L, -1);
+			lua_pop(L, 1);
+			lua_getfield(L, top_base, "reason");
+			reason = lua_tostring(L, -1);
+			if (reason == NULL)
+				reason = "";
+			lua_pop(L, 1);
+		} else if (luaL_iserror(L, top_base)) {
+			lua_error(L);
+			return;
+		}
 	} else {
 		luaL_error(L, "box.error(): bad arguments");
 	}
@@ -104,8 +102,34 @@ raise:
 	}
 	say_debug("box.error() at %s:%i", file, line);
 	box_error_set(file, line, code, "%s", reason);
-	luaT_error(L);
-	return 0;
+}
+
+static int
+luaT_error_call(lua_State *L)
+{
+	int top = lua_gettop(L);
+	if (top <= 1) {
+		/* Re-throw saved exceptions if any. */
+		if (box_error_last())
+			luaT_error(L);
+		return 0;
+	}
+	luaT_error_create(L, top, 2);
+	return luaT_error(L);
+}
+
+static int
+luaT_error_raise(lua_State *L)
+{
+	int top = lua_gettop(L);
+	if (top == 0) {
+		/* Re-throw saved exceptions if any. */
+		if (box_error_last())
+			luaT_error(L);
+		return 0;
+	}
+	luaT_error_create(L, top, 1);
+	return luaT_error(L);
 }
 
 static int
@@ -214,7 +238,7 @@ box_lua_error_init(struct lua_State *L) {
 	}
 	lua_newtable(L);
 	{
-		lua_pushcfunction(L, luaT_error_raise);
+		lua_pushcfunction(L, luaT_error_call);
 		lua_setfield(L, -2, "__call");
 
 		lua_newtable(L);
diff --git a/test/box/misc.result b/test/box/misc.result
index 57717c4fe..2102e4a1c 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -176,6 +176,45 @@ box.error(box.error.UNSUPPORTED)
 ---
 - error: 'box.error(): bad arguments'
 ...
+--
+-- box.error.raise not worked because it is __index method of
+-- box.error and box.error() is the __call method. The second
+-- one takes itself as the first argument.
+--
+box.error(box.error.CREATE_SPACE, "space", "error")
+---
+- error: 'Failed to create space ''space'': error'
+...
+box.error()
+---
+- error: 'Failed to create space ''space'': error'
+...
+box.error.raise()
+---
+- error: 'Failed to create space ''space'': error'
+...
+box.error.raise(box.error.CREATE_SPACE, "space", "error")
+---
+- error: 'Failed to create space ''space'': error'
+...
+box.error.raise(box.error.UNKNOWN)
+---
+- error: Unknown error
+...
+--
+-- Allow to rethrow error.
+--
+_, err = pcall(box.error, box.error.UNKNOWN)
+---
+...
+box.error(err)
+---
+- error: Unknown error
+...
+box.error.raise(err)
+---
+- error: Unknown error
+...
 ----------------
 -- # box.stat
 ----------------
diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua
index b7bf600c3..299dc830f 100644
--- a/test/box/misc.test.lua
+++ b/test/box/misc.test.lua
@@ -52,6 +52,24 @@ box.error(box.error.UNSUPPORTED, "x", "x%s")
 box.error(box.error.UNSUPPORTED, "x")
 box.error(box.error.UNSUPPORTED)
 
+--
+-- box.error.raise not worked because it is __index method of
+-- box.error and box.error() is the __call method. The second
+-- one takes itself as the first argument.
+--
+box.error(box.error.CREATE_SPACE, "space", "error")
+box.error()
+box.error.raise()
+box.error.raise(box.error.CREATE_SPACE, "space", "error")
+box.error.raise(box.error.UNKNOWN)
+
+--
+-- Allow to rethrow error.
+--
+_, err = pcall(box.error, box.error.UNKNOWN)
+box.error(err)
+box.error.raise(err)
+
 ----------------
 -- # box.stat
 ----------------
-- 
2.15.1 (Apple Git-101)

  reply	other threads:[~2018-04-16 18:39 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-16 18:39 [PATCH 0/8] netbox: introduce fiber-async API Vladislav Shpilevoy
2018-04-16 18:39 ` Vladislav Shpilevoy [this message]
2018-04-23 16:19   ` [PATCH 1/8] lua: fix box.error.raise Vladimir Davydov
2018-05-08 15:36   ` [tarantool-patches] " Konstantin Osipov
2018-05-08 17:24     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-16 18:39 ` [PATCH 2/8] lua: allow to create and error object with no throw Vladislav Shpilevoy
2018-04-23 16:20   ` Vladimir Davydov
2018-05-08 15:37   ` [tarantool-patches] " Konstantin Osipov
2018-04-16 18:39 ` [PATCH 3/8] console: fix a bug in interactive readline usage Vladislav Shpilevoy
2018-04-23 16:20   ` Vladimir Davydov
2018-05-08 15:37   ` [tarantool-patches] " Konstantin Osipov
2018-04-16 18:39 ` [PATCH 4/8] netbox: extend codec with 'decode' methods Vladislav Shpilevoy
2018-04-23 16:42   ` Vladimir Davydov
2018-04-23 18:59     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-24 13:16       ` Vladimir Davydov
2018-05-08 15:49   ` [tarantool-patches] " Konstantin Osipov
2018-05-08 17:24     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-16 18:39 ` [PATCH 5/8] test: fix unstable test Vladislav Shpilevoy
2018-04-22  5:32   ` [tarantool-patches] " Kirill Yukhin
2018-05-08 15:50   ` Konstantin Osipov
2018-04-16 18:39 ` [PATCH 6/8] netbox: introduce fiber-async API Vladislav Shpilevoy
2018-04-23 12:31   ` [tarantool-patches] " Alexander Turenko
2018-04-23 18:59     ` Vladislav Shpilevoy
2018-04-23 16:44   ` Vladimir Davydov
2018-04-23 18:59     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-24 13:05       ` Vladimir Davydov
2018-04-16 18:39 ` [PATCH 7/8] netbox: remove schema_version from requests Vladislav Shpilevoy
2018-05-08 16:06   ` [tarantool-patches] " Konstantin Osipov
2018-05-08 17:24     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-16 18:39 ` [PATCH 8/8] netbox: implement perform_request via async version Vladislav Shpilevoy
2018-04-23 16:47   ` Vladimir Davydov
2018-04-23 19:00     ` [tarantool-patches] " Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=860da7f4e8760673d6052614a5df1d4bdb67c06b.1523903144.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH 1/8] lua: fix box.error.raise' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox