Skip to content

Commit a19aac8

Browse files
CuriousGeorgiysergepetrenko
authored andcommitted
lua: add message argument to table constructor of box.error.new
Currently, the error message is specified in the `reason` argument to the table constructor of `box.error.new`. This is confusing, because the message is accessed and printed by `error:unpack` as `message`. Let’s allow users to pass the error message in the `message` argument. We won’t drop the `reason` argument so as not to break potential users. Let's also allow to omit the message key by treating the first table constructor entry as an error message if present. The error message setting has the following priority: first table constructor entry > `message` > `reason`. This change is backwards-compatible and does not require a new `compat` option. Closes #9102 @TarantoolBot document Title: Document `message` 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#c984e766372743c99a4a7be79a5783f6)
1 parent c799fc5 commit a19aac8

File tree

3 files changed

+42
-4
lines changed

3 files changed

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

src/box/lua/error.cc

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ luaT_error_add_payload(lua_State *L, struct error *error)
7878
region_truncate(gc, used);
7979
}
8080

81+
/**
82+
* In case the error is constructed from a table, retrieves the reason.
83+
*
84+
* @param L Lua state
85+
* @param index table index on Lua stack
86+
*
87+
* @return reason
88+
* @retval "" failed to retrieve reason
89+
*/
90+
static const char *
91+
error_create_table_case_get_reason(lua_State *L, int index)
92+
{
93+
lua_rawgeti(L, index, 1);
94+
const char *reason = lua_tostring(L, -1);
95+
if (reason != NULL)
96+
return reason;
97+
lua_getfield(L, index, "message");
98+
reason = lua_tostring(L, -1);
99+
if (reason != NULL)
100+
return reason;
101+
lua_getfield(L, index, "reason");
102+
reason = lua_tostring(L, -1);
103+
return reason != NULL ? reason : "";
104+
}
105+
81106
/**
82107
* Parse Lua arguments (they can come as single table or as
83108
* separate members) and construct struct error with given values.
@@ -146,10 +171,7 @@ luaT_error_create(lua_State *L, int top_base)
146171
lua_getfield(L, top_base, "code");
147172
if (!lua_isnil(L, -1))
148173
code = lua_tonumber(L, -1);
149-
lua_getfield(L, top_base, "reason");
150-
reason = lua_tostring(L, -1);
151-
if (reason == NULL)
152-
reason = "";
174+
reason = error_create_table_case_get_reason(L, top_base);
153175
lua_getfield(L, top_base, "type");
154176
if (!lua_isnil(L, -1))
155177
custom_type = lua_tostring(L, -1);

test/box-luatest/error_subsystem_improvements_test.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,15 @@ g.test_custom_payload = function()
7979
custom_type = "MyAppError",
8080
})
8181
end
82+
83+
-- Test the `message` argument to the table constructor of `box.error.new`
84+
-- (gh-9102).
85+
g.test_error_new_message_arg = function()
86+
local msg = 'message'
87+
t.assert_equals(box.error.new{msg}.message, msg)
88+
t.assert_equals(box.error.new{message = msg}.message, msg)
89+
t.assert_equals(box.error.new{reason = msg}.message, msg)
90+
t.assert_equals(box.error.new{msg, message = 'other'}.message, msg)
91+
t.assert_equals(box.error.new{message = msg, reason = 'other'}.message, msg)
92+
t.assert_equals(box.error.new{msg, reason = 'other'}.message, msg)
93+
end

0 commit comments

Comments
 (0)