Skip to content
Snippets Groups Projects
Commit 8213d07c authored by Konstantin Osipov's avatar Konstantin Osipov
Browse files

Merge branch 'box.raise'

parents 6af4ff09 0acdd629
No related branches found
No related tags found
No related merge requests found
......@@ -891,6 +891,36 @@ lua box.dostring('local f = function(key) t=box.select(0, 0, key); if t ~= nil t
</programlisting>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis role="lua">box.raise(errcode, errtext)</emphasis>
</term>
<listitem>
<para>
Raises a client error. The difference between this function
and the built-in <code>error()</code> function in Lua
is that when the error reaches the client, it's error code
is preserved, whereas every Lua error is presented to the
client as <constant>ER_PROC_LUA</constant>. This function
makes it possible to emulate any kind of native exception,
such as a unique constraint violation, no such space/index,
etc. A complete list of errors is present in <link xlink:href="https://github.com/mailru/tarantool/blob/master/include/errcode.h">errcode.h</link>
file in the source tree.
Lua constants which correspond to Tarantool/Box errors
are defined in <code>box.error</code> module. The error
message can be arbitrary.
Throws client error. Lua procedure can emulate any
request errors (for example: unique key exception).
</para>
<bridgehead renderas="sect4">Example</bridgehead>
<programlisting>
lua box.raise(box.error.ER_WAL_IO, 'Wal I/O error')
---
error: 'Wal I/O error'
...
</programlisting>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis role="lua">box.auto_increment(space_no, ...)</emphasis>
......
......@@ -75,6 +75,7 @@
}
- (id) init: (uint32_t)errcode_, ...;
- (id) init: (uint32_t)errcode_ :(const char *)msg;
- (id) init: (uint32_t)errcode_ args :(va_list)ap;
- (void) log;
- (const char *) errmsg;
......
......@@ -1147,8 +1147,22 @@ lbox_process(lua_State *L)
return lua_gettop(L) - top;
}
static int
lbox_raise(lua_State *L)
{
if (lua_gettop(L) != 2)
luaL_error(L, "box.raise(): bad arguments");
uint32_t code = lua_tointeger(L, 1);
if (!code)
luaL_error(L, "box.raise(): unknown error code");
const char *str = lua_tostring(L, 2);
tnt_raise(ClientError, :code :str);
return 0;
}
static const struct luaL_reg boxlib[] = {
{"process", lbox_process},
{"raise", lbox_raise},
{NULL, NULL}
};
......
......@@ -113,6 +113,14 @@
return self;
}
- (id) init: (uint32_t)errcode_ :(const char *)msg
{
[super init];
errcode = errcode_;
snprintf(errmsg, sizeof(errmsg), "%s", msg);
return self;
}
- (void) log
{
say_error("%s at %s:%d, %s", object_getClassName(self),
......
No preview for this file type
#
# box.raise
#
lua 1 + 1
---
- 2
...
lua box.raise(123, 'test')
---
error: 'test'
...
lua box.raise(0, 'the other test')
---
error: 'box.raise(): unknown error code'
...
lua box.raise(12, 345)
---
error: '345'
...
#
# box.stat
#
lua for k, v in pairs(box.stat()) do print(k) end
---
DELETE
SELECT
REPLACE
CALL
UPDATE
DELETE_1_3
...
lua for k, v in pairs(box.stat().DELETE) do print(k) end
---
total
rps
...
lua for k, v in pairs(box.stat.DELETE) do print(k) end
---
total
rps
...
#
# box.space
#
lua type(box)
---
- table
...
lua type(box.space)
---
- table
...
lua box.cfg.memcached_space
---
- 23
...
lua for i, v in pairs(box.space[0].index[0].key_field[0]) do print(i, ': ', v) end
---
type: NUM
fieldno: 0
...
#
# box.space
#
lua string.match(tostring(box.slab), '^table:') ~= nil
---
- true
......
# encoding: tarantool
#
import sys
print """
#
# box.raise
#
"""
exec admin "lua 1 + 1"
exec admin "lua box.raise(123, 'test')"
exec admin "lua box.raise(0, 'the other test')"
exec admin "lua box.raise(12, 345)"
print """
#
# box.stat
#
"""
exec admin "lua for k, v in pairs(box.stat()) do print(k) end"
exec admin "lua for k, v in pairs(box.stat().DELETE) do print(k) end"
exec admin "lua for k, v in pairs(box.stat.DELETE) do print(k) end"
print """
#
# box.space
#
"""
exec admin "lua type(box)"
exec admin "lua type(box.space)"
exec admin "lua box.cfg.memcached_space"
exec admin "lua for i, v in pairs(box.space[0].index[0].key_field[0]) do print(i, ': ', v) end"
print """
#
# box.space
#
"""
exec admin "lua string.match(tostring(box.slab), '^table:') ~= nil"
exec admin "lua box.slab.arena_used >= 0"
......
lua type(box)
---
- table
...
lua type(box.space)
---
- table
...
lua box.cfg.memcached_space
---
- 23
...
lua for i, v in pairs(box.space[0].index[0].key_field[0]) do print(i, ': ', v) end
---
type: NUM
fieldno: 0
...
# encoding: tarantool
exec admin "lua type(box)"
exec admin "lua type(box.space)"
exec admin "lua box.cfg.memcached_space"
exec admin "lua for i, v in pairs(box.space[0].index[0].key_field[0]) do print(i, ': ', v) end"
lua for k, v in pairs(box.stat()) do print(k) end
---
DELETE
SELECT
REPLACE
CALL
UPDATE
DELETE_1_3
...
lua for k, v in pairs(box.stat().DELETE) do print(k) end
---
total
rps
...
lua for k, v in pairs(box.stat.DELETE) do print(k) end
---
total
rps
...
# encoding: tarantool
import os
import sys
exec admin "lua for k, v in pairs(box.stat()) do print(k) end"
exec admin "lua for k, v in pairs(box.stat().DELETE) do print(k) end"
exec admin "lua for k, v in pairs(box.stat.DELETE) do print(k) end"
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