Skip to content
Snippets Groups Projects
Commit f00945a1 authored by Nikita Pettik's avatar Nikita Pettik
Browse files

box/error: introduce box.error.set() method

box.error.set(err) sets err to instance's diagnostics area. Argument err
is supposed to be instance of error object. This method is required
since we are going to avoid adding created via box.error.new() errors to
Tarantool's diagnostic area.

Needed for #1148
Part of #4778
parent 195d99d0
No related branches found
No related tags found
No related merge requests found
...@@ -154,6 +154,16 @@ luaT_error_clear(lua_State *L) ...@@ -154,6 +154,16 @@ luaT_error_clear(lua_State *L)
return 0; return 0;
} }
static int
luaT_error_set(struct lua_State *L)
{
if (lua_gettop(L) == 0)
return luaL_error(L, "Usage: box.error.set(error)");
struct error *e = luaL_checkerror(L, 1);
diag_set_error(&fiber()->diag, e);
return 0;
}
static int static int
lbox_errinj_set(struct lua_State *L) lbox_errinj_set(struct lua_State *L)
{ {
...@@ -268,6 +278,10 @@ box_lua_error_init(struct lua_State *L) { ...@@ -268,6 +278,10 @@ box_lua_error_init(struct lua_State *L) {
lua_pushcfunction(L, luaT_error_new); lua_pushcfunction(L, luaT_error_new);
lua_setfield(L, -2, "new"); lua_setfield(L, -2, "new");
} }
{
lua_pushcfunction(L, luaT_error_set);
lua_setfield(L, -2, "set");
}
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
} }
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
......
...@@ -53,7 +53,7 @@ luaL_iserror(struct lua_State *L, int narg) ...@@ -53,7 +53,7 @@ luaL_iserror(struct lua_State *L, int narg)
return e; return e;
} }
static struct error * struct error *
luaL_checkerror(struct lua_State *L, int narg) luaL_checkerror(struct lua_State *L, int narg)
{ {
struct error *error = luaL_iserror(L, narg); struct error *error = luaL_iserror(L, narg);
......
...@@ -65,6 +65,9 @@ luaT_pusherror(struct lua_State *L, struct error *e); ...@@ -65,6 +65,9 @@ luaT_pusherror(struct lua_State *L, struct error *e);
struct error * struct error *
luaL_iserror(struct lua_State *L, int narg); luaL_iserror(struct lua_State *L, int narg);
struct error *
luaL_checkerror(struct lua_State *L, int narg);
void void
tarantool_lua_error_init(struct lua_State *L); tarantool_lua_error_init(struct lua_State *L);
......
...@@ -436,6 +436,42 @@ test_run:cmd("setopt delimiter ''"); ...@@ -436,6 +436,42 @@ test_run:cmd("setopt delimiter ''");
| --- | ---
| - true | - true
| ... | ...
-- gh-4778: don't add created via box.error.new() errors to
-- Tarantool's diagnostic area.
--
err = box.error.new({code = 111, reason = "cause"})
| ---
| ...
assert(box.error.last() ~= err)
| ---
| - error: assertion failed!
| ...
box.error.set(err)
| ---
| ...
assert(box.error.last() == err)
| ---
| - true
| ...
-- Consider wrong or tricky inputs to box.error.set()
--
box.error.set(1)
| ---
| - error: 'Invalid argument #1 (error expected, got number)'
| ...
box.error.set(nil)
| ---
| - error: 'Invalid argument #1 (error expected, got nil)'
| ...
box.error.set(box.error.last())
| ---
| ...
assert(box.error.last() == err)
| ---
| - true
| ...
space:drop() space:drop()
| --- | ---
| ... | ...
...@@ -77,4 +77,19 @@ end; ...@@ -77,4 +77,19 @@ end;
t; t;
test_run:cmd("setopt delimiter ''"); test_run:cmd("setopt delimiter ''");
-- gh-4778: don't add created via box.error.new() errors to
-- Tarantool's diagnostic area.
--
err = box.error.new({code = 111, reason = "cause"})
assert(box.error.last() ~= err)
box.error.set(err)
assert(box.error.last() == err)
-- Consider wrong or tricky inputs to box.error.set()
--
box.error.set(1)
box.error.set(nil)
box.error.set(box.error.last())
assert(box.error.last() == err)
space:drop() space:drop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment