Skip to content

Commit

Permalink
lua: add error autocompletion
Browse files Browse the repository at this point in the history
Let's add an `__autocomplete` method to enable error autocompletion and
make the error object easier to use. It will suggest error object fields
(including own and inherited payload fields) and methods.

Closes #9107

NO_DOC=<not a documentable feature>
  • Loading branch information
CuriousGeorgiy authored and sergepetrenko committed Mar 14, 2024
1 parent cf644ab commit 31b099a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelogs/unreleased/gh-9107-error-autocomplete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## feature/lua

* Added autocompletion (including payload fields) to `box.error` (gh-9107).
26 changes: 25 additions & 1 deletion src/lua/error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,36 @@ local function error_serialize(err)
end
end

local error_methods = {
local error_methods

local function error_autocomplete(err)
local err_unpacked = {}

local cur_err = err
while cur_err ~= nil do
for key, val in pairs(cur_err:unpack()) do
if err_unpacked[key] == nil then
err_unpacked[key] = val
end
end
cur_err = cur_err.prev
end

for key, method in pairs(error_methods) do
if not key:startswith('__') then
err_unpacked[key] = method
end
end
return err_unpacked
end

error_methods = {
["unpack"] = error_unpack;
["raise"] = error_raise;
["match"] = error_match; -- Tarantool 1.6 backward compatibility
["__serialize"] = error_serialize;
["set_prev"] = error_set_prev;
["__autocomplete"] = error_autocomplete;
}

local function error_index(err, key)
Expand Down
42 changes: 42 additions & 0 deletions test/box-luatest/error_subsystem_improvements_test.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local t = require('luatest')
local compat = require('compat')
local console = require('console')
local json = require('json')
local yaml = require('yaml')
local tarantool = require('tarantool')
Expand Down Expand Up @@ -270,3 +271,44 @@ g.test_client_error_creation = function()
local e = box.error.new(box.error.TEST_TYPE_INT, 1, 2, 3, 4, 5)
t.assert_equals(e.field, 1)
end

--
-- Get tab completion for @s
-- @param s string
-- @return tab completion
local function tabcomplete(s)
return console.completion_handler(s, 0, #s)
end

-- Test `box.error` autocompletion (gh-9107).
g.test_autocomplete = function()
-- tabcomplete always uses global table
rawset(_G, 'err', box.error.new{'cur', foo = 777,
prev = box.error.new{'prev', bar = 777}})

local r = tabcomplete('err.')
t.assert_items_equals(r, {
'err.',
'err.raise(',
'err.foo',
'err.base_type',
'err.prev',
'err.message',
'err.match(',
'err.set_prev(',
'err.trace',
'err.type',
'err.unpack(',
'err.code',
'err.bar',
})

r = tabcomplete('err:')
t.assert_items_equals(r, {
'err:',
'err:match(',
'err:raise(',
'err:set_prev(',
'err:unpack(',
})
end

0 comments on commit 31b099a

Please sign in to comment.