Skip to content

Commit d592f26

Browse files
Gumixsergepetrenko
authored andcommitted
box/lua: implement inheritance of error payload fields
Suppose an error has a cause with some payload fields, for example: local e1 = box.error.new{'e1', foo = 'bar'} -- cause local e2 = box.error.new{'e2', prev = e1} -- effect Now it is possible to access cause payload fields via e2 directly: e2.foo -- 'bar' While looking for a payload field with a given name, we always stop at the topmost (closest to the effect) field. If there's a field with the same name deeper in the stack it is masked. Closes tarantool#9106 @TarantoolBot document Title: Document inheritance of error payload fields Product: Tarantool Since: 3.1 Root document: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_error/error_object/ [Link to the design document](https://www.notion.so/tarantool/Error-subsystem-improvements-90faa0a4714b4143abaf8bed2c10b2fc?pvs=4#c080fe2ac28b46c8b0eda7234a8852ce)
1 parent 709938a commit d592f26

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
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+
* Now it is possible to access a payload field of an error's cause directly
4+
from the error (gh-9106).

src/lua/error.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,14 @@ local function error_index(err, key)
182182
if getter ~= nil then
183183
return getter(err)
184184
end
185-
local f = ffi.C.error_find_field(err, key)
186-
if f ~= nil then
187-
return mp_decode(f._data)
185+
-- Look for a payload field, starting from the topmost error in the stack.
186+
local cur_err = err
187+
while cur_err ~= nil do
188+
local f = ffi.C.error_find_field(cur_err, key)
189+
if f ~= nil then
190+
return mp_decode(f._data)
191+
end
192+
cur_err = cur_err.prev
188193
end
189194
end
190195

test/box-luatest/error_subsystem_improvements_test.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,14 @@ g.test_methods_masking = function()
127127
t.assert_equals(e:unpack().set_prev, 'hack')
128128
t.assert_equals(e:unpack().__serialize, 'hack')
129129
end
130+
131+
-- Test that payload fields of a cause are inherited by the effect (gh-9106).
132+
g.test_payload_inheritance = function()
133+
local e1 = box.error.new({foo = 11, bar = 22})
134+
local e2 = box.error.new({bar = 33, baz = 44, prev = e1})
135+
local e3 = box.error.new({baz = 55, prev = e2})
136+
137+
t.assert_equals(e3.foo, 11) -- Inherited from e1.
138+
t.assert_equals(e3.bar, 33) -- Inherited from e2.
139+
t.assert_equals(e3.baz, 55) -- Own payload field.
140+
end

0 commit comments

Comments
 (0)