Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@dev.tarantool.org
Cc: v.shpilevoy@tarantool.org
Subject: [Tarantool-patches] [PATCH 2/7] box/error: introduce box.error.set() method
Date: Wed, 19 Feb 2020 17:16:51 +0300	[thread overview]
Message-ID: <0eca2c9457d95dbef38556d738f1e5818468a175.1582119629.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1582119629.git.korablev@tarantool.org>
In-Reply-To: <cover.1582119629.git.korablev@tarantool.org>

box.error.set(err) sets err to instance's diagnostics area. Argument err
is supposed to be instance of error object. This method is required
since we are going to avoid adding created via box.error.new() errors to
Tarantool's diagnostic area.

Needed for #1148
Part of #4778
---
 src/box/lua/error.cc   | 15 +++++++++++++++
 src/lua/error.c        |  2 +-
 src/lua/error.h        |  3 +++
 test/box/misc.result   | 34 ++++++++++++++++++++++++++++++++++
 test/box/misc.test.lua | 14 ++++++++++++++
 5 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/src/box/lua/error.cc b/src/box/lua/error.cc
index fc53a40f4..7311cb2ce 100644
--- a/src/box/lua/error.cc
+++ b/src/box/lua/error.cc
@@ -154,6 +154,17 @@ luaT_error_clear(lua_State *L)
 	return 0;
 }
 
+static int
+luaT_error_set(lua_State *L)
+{
+	if (lua_gettop(L) == 0)
+		return luaL_error(L, "Usage: box.error.set(error)");
+	struct error *e = luaL_checkerror(L, 1);
+	assert(e != NULL);
+	diag_set_error(&fiber()->diag, e);
+	return 0;
+}
+
 static int
 lbox_errinj_set(struct lua_State *L)
 {
@@ -268,6 +279,10 @@ box_lua_error_init(struct lua_State *L) {
 			lua_pushcfunction(L, luaT_error_new);
 			lua_setfield(L, -2, "new");
 		}
+		{
+			lua_pushcfunction(L, luaT_error_set);
+			lua_setfield(L, -2, "set");
+		}
 		lua_setfield(L, -2, "__index");
 	}
 	lua_setmetatable(L, -2);
diff --git a/src/lua/error.c b/src/lua/error.c
index d82e78dc4..18a990a88 100644
--- a/src/lua/error.c
+++ b/src/lua/error.c
@@ -53,7 +53,7 @@ luaL_iserror(struct lua_State *L, int narg)
 	return e;
 }
 
-static struct error *
+struct error *
 luaL_checkerror(struct lua_State *L, int narg)
 {
 	struct error *error = luaL_iserror(L, narg);
diff --git a/src/lua/error.h b/src/lua/error.h
index 64fa5eba3..16cdaf7fe 100644
--- a/src/lua/error.h
+++ b/src/lua/error.h
@@ -65,6 +65,9 @@ luaT_pusherror(struct lua_State *L, struct error *e);
 struct error *
 luaL_iserror(struct lua_State *L, int narg);
 
+struct error *
+luaL_checkerror(struct lua_State *L, int narg);
+
 void
 tarantool_lua_error_init(struct lua_State *L);
 
diff --git a/test/box/misc.result b/test/box/misc.result
index 5ac5e0f26..48c00aadc 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -290,6 +290,40 @@ type(err.errno)
 ---
 - nil
 ...
+-- gh-4778: don't add created via box.error.new() errors to
+-- Tarantool's diagnostic area.
+--
+err = box.error.new({code = 111, reason = "cause"})
+---
+...
+assert(box.error.last() ~= err)
+---
+- error: assertion failed!
+...
+box.error.set(err)
+---
+...
+assert(box.error.last() == err)
+---
+- true
+...
+-- Consider wrong or tricky inputs to box.error.set()
+--
+box.error.set(1)
+---
+- error: 'Invalid argument #1 (error expected, got number)'
+...
+box.error.set(nil)
+---
+- error: 'Invalid argument #1 (error expected, got nil)'
+...
+box.error.set(box.error.last())
+---
+...
+assert(box.error.last() == err)
+---
+- true
+...
 ----------------
 -- # box.stat
 ----------------
diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua
index e1c2f990f..cb7b775fc 100644
--- a/test/box/misc.test.lua
+++ b/test/box/misc.test.lua
@@ -91,6 +91,20 @@ type(err.errno)
 err = box.error.new(box.error.PROC_LUA, "errno")
 type(err.errno)
 
+-- gh-4778: don't add created via box.error.new() errors to
+-- Tarantool's diagnostic area.
+--
+err = box.error.new({code = 111, reason = "cause"})
+assert(box.error.last() ~= err)
+box.error.set(err)
+assert(box.error.last() == err)
+-- Consider wrong or tricky inputs to box.error.set()
+--
+box.error.set(1)
+box.error.set(nil)
+box.error.set(box.error.last())
+assert(box.error.last() == err)
+
 ----------------
 -- # box.stat
 ----------------
-- 
2.15.1

  parent reply	other threads:[~2020-02-19 14:17 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-19 14:16 [Tarantool-patches] [PATCH 0/7] Stacked diagnostics area Nikita Pettik
2020-02-19 14:16 ` [Tarantool-patches] [PATCH 1/7] box: rename diag_add_error to diag_set_error Nikita Pettik
2020-02-19 14:16 ` Nikita Pettik [this message]
2020-02-19 14:26   ` [Tarantool-patches] [PATCH 2/7] box/error: introduce box.error.set() method Cyrill Gorcunov
2020-02-19 14:30     ` Nikita Pettik
2020-02-19 14:53       ` Cyrill Gorcunov
2020-02-19 14:16 ` [Tarantool-patches] [PATCH 3/7] box/error: don't set error created via box.error.new to diag Nikita Pettik
2020-02-22 17:18   ` Vladislav Shpilevoy
2020-03-25  1:02     ` Nikita Pettik
2020-03-26  0:22       ` Vladislav Shpilevoy
2020-03-26  1:03         ` Nikita Pettik
2020-02-19 14:16 ` [Tarantool-patches] [PATCH 4/7] box: introduce stacked diagnostic area Nikita Pettik
2020-02-19 21:10   ` Vladislav Shpilevoy
2020-02-20 11:53     ` Nikita Pettik
2020-02-20 18:29       ` Nikita Pettik
2020-02-23 17:43   ` Vladislav Shpilevoy
2020-03-25  1:34     ` Nikita Pettik
2020-02-19 14:16 ` [Tarantool-patches] [PATCH 5/7] box/error: clarify purpose of reference counting in struct error Nikita Pettik
2020-02-23 17:43   ` Vladislav Shpilevoy
2020-03-25  1:40     ` Nikita Pettik
2020-02-19 14:16 ` [Tarantool-patches] [PATCH 6/7] iproto: refactor error encoding with mpstream Nikita Pettik
2020-02-23 17:44   ` Vladislav Shpilevoy
2020-03-25  1:42     ` Nikita Pettik
2020-02-19 14:16 ` [Tarantool-patches] [PATCH 7/7] iproto: support error stacked diagnostic area Nikita Pettik
2020-02-23 17:43   ` Vladislav Shpilevoy
2020-03-25  1:38     ` Nikita Pettik
2020-02-22 17:18 ` [Tarantool-patches] [PATCH 0/7] Stacked diagnostics area 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=0eca2c9457d95dbef38556d738f1e5818468a175.1582119629.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 2/7] box/error: introduce box.error.set() method' \
    /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