Skip to content

Commit 885a3d7

Browse files
Gumixsergepetrenko
authored andcommitted
box/lua: allow to set error cause via prev argument
Currently, it's only possible to set an error cause with the set_prev method. This isn't very convenient, because one has to construct a new error without raising it, then set its cause, and only then raise it. To simplify this, a new argument `prev` is added to the error constructor. Closes tarantool#9103 @TarantoolBot document Title: Document `prev` argument to table constructor of `box.error.new` Product: Tarantool Since: 3.1 Root documents: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_error/new/ and https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_error/error/ [Link to the design document](https://www.notion.so/tarantool/Error-subsystem-improvements-90faa0a4714b4143abaf8bed2c10b2fc?pvs=4#c72c870f24734020aae1fbf34e2b8569)
1 parent 4043664 commit 885a3d7

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## feature/lua
2+
3+
* Added a `prev` argument to the table constructor of `box.error.new` (gh-9103).

src/box/lua/error.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ luaT_error_create(lua_State *L, int top_base)
129129
const char *file = "";
130130
unsigned line = 0;
131131
lua_Debug info;
132+
struct error *prev = NULL;
132133
int top = lua_gettop(L);
133134
int top_type = lua_type(L, top_base);
134135
if (top >= top_base && (top_type == LUA_TNUMBER ||
@@ -175,6 +176,15 @@ luaT_error_create(lua_State *L, int top_base)
175176
lua_getfield(L, top_base, "type");
176177
if (!lua_isnil(L, -1))
177178
custom_type = lua_tostring(L, -1);
179+
lua_getfield(L, top_base, "prev");
180+
if (!lua_isnil(L, -1)) {
181+
prev = luaL_iserror(L, -1);
182+
if (prev == NULL) {
183+
luaL_error(L, "Invalid argument 'prev' (error "
184+
"expected, got %s)",
185+
lua_typename(L, lua_type(L, -1)));
186+
}
187+
}
178188
} else {
179189
return NULL;
180190
}
@@ -192,6 +202,13 @@ luaT_error_create(lua_State *L, int top_base)
192202
}
193203
struct error *error;
194204
error = box_error_new(file, line, code, custom_type, "%s", reason);
205+
/*
206+
* Set the previous error, if it was specified.
207+
*/
208+
if (prev != NULL) {
209+
/* Cycle is not possible for the newly created error. */
210+
VERIFY(error_set_prev(error, prev) == 0);
211+
}
195212
/*
196213
* Add custom payload fields to the `error' if any.
197214
*/

test/box-luatest/error_subsystem_improvements_test.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
local t = require('luatest')
22
local g = t.group()
33

4+
-- Test the `prev' argument to the table constructor of `box.error.new'
5+
-- (gh-9103).
6+
g.test_error_new_prev_arg = function()
7+
local e1 = box.error.new({"errare"})
8+
local e2 = box.error.new({"humanum", prev = e1})
9+
local _, e3 = pcall(box.error, {"est", prev = e2})
10+
11+
t.assert_equals(e3.prev.prev.message, "errare")
12+
t.assert_equals(e3.prev.message, "humanum")
13+
t.assert_equals(e3.message, "est")
14+
15+
t.assert_error_msg_equals(
16+
"Invalid argument 'prev' (error expected, got number)",
17+
box.error.new, {prev = 333})
18+
end
19+
420
-- Test custom error payload (gh-9104).
521
g.test_custom_payload = function()
622
local uuid = require('uuid')
@@ -67,7 +83,6 @@ g.test_custom_payload = function()
6783
custom_type = "MyCustomType",
6884
errno = "MyErrno",
6985
trace = "MyTrace",
70-
prev = "MyPrev",
7186
})
7287
unpacked = e:unpack()
7388
unpacked.trace = nil

0 commit comments

Comments
 (0)