Skip to content

Commit 3d5726b

Browse files
authored
Optimize errors wrap (#43)
* fix assign to undeclared variable error Tarantool in debug build has strict mode enabled by default. As result before this patch we faced an error "assign to undeclared variable". * fix error messages for new Tarantool versions - netbox timeout error was changed (tarantool/tarantool#6538) - error on connection refused was extended * optimize errors.wrap for one object case Simple errors.wrap loop. After patch: 0.000611 Before patch: 0.094577 The main reason - we could eliminate table creation in case if only one object should be wrapped.
1 parent 02f4a85 commit 3d5726b

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

errors.lua

+26-21
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ function error_class:new(...)
119119
return e
120120
end
121121

122-
local function pack(...)
123-
return select('#', ...), {...}
124-
end
125-
126122
local function pcall_tail(status, ...)
127123
if not status then
128124
return nil, ...
@@ -297,25 +293,34 @@ local function string_format(fmt)
297293
end
298294
end
299295

296+
local function _wrap_one(lvl, prefix_format, suffix_format, obj)
297+
if obj == box.NULL then
298+
return nil
299+
elseif is_error_object(obj) then
300+
if prefix_format ~= nil then
301+
local error_prefix = string_format(prefix_format)
302+
obj.err = error_prefix .. ': ' .. obj.err
303+
obj.str = obj.class_name .. ': ' .. obj.err
304+
end
305+
306+
if restore_mt(obj) and obj.stack ~= nil then
307+
local local_stack = string.strip(debug.traceback("", lvl))
308+
local stack_suffix = string_format(suffix_format)
309+
obj.stack = obj.stack .. '\n' .. stack_suffix .. '\n' .. local_stack
310+
end
311+
end
312+
return obj
313+
end
314+
300315
local function _wrap(prefix_format, suffix_format, ...)
301-
local n, ret = pack(...)
316+
local n = select('#', ...)
317+
if n == 1 then
318+
return _wrap_one(2, prefix_format, suffix_format, ...)
319+
end
320+
321+
local ret = {...}
302322
for i = 1, n do
303-
local obj = ret[i]
304-
if obj == box.NULL then
305-
ret[i] = nil
306-
elseif is_error_object(obj) then
307-
if prefix_format ~= nil then
308-
local error_prefix = string_format(prefix_format)
309-
obj.err = error_prefix .. ': ' .. obj.err
310-
obj.str = obj.class_name .. ': ' .. obj.err
311-
end
312-
313-
if restore_mt(obj) and obj.stack ~= nil then
314-
local local_stack = string.strip(debug.traceback("", 2))
315-
local stack_suffix = string_format(suffix_format)
316-
obj.stack = obj.stack .. '\n' .. stack_suffix .. '\n' .. local_stack
317-
end
318-
end
323+
ret[i] = _wrap_one(3, prefix_format, suffix_format, ret[i])
319324
end
320325

321326
return unpack(ret, 1, n)

0 commit comments

Comments
 (0)