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 payload fields) and methods.

Closes tarantool#9107

NO_DOC=<not a documentable feature>
  • Loading branch information
CuriousGeorgiy committed Mar 4, 2024
1 parent 69216e2 commit 3436e10
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
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).
12 changes: 12 additions & 0 deletions src/lua/error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ local error_methods = {
["set_prev"] = error_set_prev;
}

local function error_autocomplete(err)
local err_unpacked = err:unpack()
for key, method in pairs(error_methods) do
if key:match('__.+') == nil then
err_unpacked[key] = method
end
end
return err_unpacked
end

error_methods["__autocomplete"] = error_autocomplete

local function error_index(err, key)
local method = error_methods[key]
if method ~= nil then
Expand Down
38 changes: 38 additions & 0 deletions test/box-luatest/error_subsystem_improvements_test.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local t = require('luatest')
local console = require('console')

local g = t.group()

-- Test the `prev' argument to the table constructor of `box.error.new'
Expand Down Expand Up @@ -138,3 +140,39 @@ g.test_payload_inheritance = function()
t.assert_equals(e3.bar, 33) -- Inherited from e2.
t.assert_equals(e3.baz, 55) -- Own payload field.
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{'message', foo = 777})

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

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

0 comments on commit 3436e10

Please sign in to comment.