[PATCH 2/8] lua: allow to create and error object with no throw

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Mon Apr 16 21:39:12 MSK 2018


It is needed to return error via 'nil, error_object' notation,
and to store an error object to return it later.

Closes #3031
---
 src/box/lua/error.cc   | 15 +++++++++++++++
 test/box/misc.result   | 25 +++++++++++++++++++++++++
 test/box/misc.test.lua | 10 ++++++++++
 3 files changed, 50 insertions(+)

diff --git a/src/box/lua/error.cc b/src/box/lua/error.cc
index 56cc2c563..2a56f1a12 100644
--- a/src/box/lua/error.cc
+++ b/src/box/lua/error.cc
@@ -148,6 +148,17 @@ luaT_error_last(lua_State *L)
 	return 1;
 }
 
+static int
+luaT_error_new(lua_State *L)
+{
+	int top = lua_gettop(L);
+	if (top == 0)
+		return luaL_error(L, "Usage: box.error.new(code, args)");
+	luaT_error_create(L, top, 1);
+	lua_settop(L, 0);
+	return luaT_error_last(L);
+}
+
 static int
 luaT_error_clear(lua_State *L)
 {
@@ -254,6 +265,10 @@ box_lua_error_init(struct lua_State *L) {
 			lua_pushcfunction(L, luaT_error_raise);
 			lua_setfield(L, -2, "raise");
 		}
+		{
+			lua_pushcfunction(L, luaT_error_new);
+			lua_setfield(L, -2, "new");
+		}
 		lua_setfield(L, -2, "__index");
 	}
 	lua_setmetatable(L, -2);
diff --git a/test/box/misc.result b/test/box/misc.result
index 2102e4a1c..4b0f0e53d 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -215,6 +215,31 @@ box.error.raise(err)
 ---
 - error: Unknown error
 ...
+--
+-- gh-3031: allow to create an error object with no throwing it.
+--
+e = box.error.new(box.error.UNKNOWN)
+---
+...
+e
+---
+- Unknown error
+...
+e = box.error.new(box.error.CREATE_SPACE, "space", "error")
+---
+...
+e
+---
+- 'Failed to create space ''space'': error'
+...
+box.error.new()
+---
+- error: 'Usage: box.error.new(code, args)'
+...
+box.error.raise()
+---
+- error: 'Failed to create space ''space'': error'
+...
 ----------------
 -- # box.stat
 ----------------
diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua
index 299dc830f..33900d24e 100644
--- a/test/box/misc.test.lua
+++ b/test/box/misc.test.lua
@@ -70,6 +70,16 @@ _, err = pcall(box.error, box.error.UNKNOWN)
 box.error(err)
 box.error.raise(err)
 
+--
+-- gh-3031: allow to create an error object with no throwing it.
+--
+e = box.error.new(box.error.UNKNOWN)
+e
+e = box.error.new(box.error.CREATE_SPACE, "space", "error")
+e
+box.error.new()
+box.error.raise()
+
 ----------------
 -- # box.stat
 ----------------
-- 
2.15.1 (Apple Git-101)




More information about the Tarantool-patches mailing list