diff --git a/include/exception.h b/include/exception.h index f226f6b8709416586e059dcb3ce647be15469838..7f23523e543b21ef8955f4ccf7018b6d8e40616a 100644 --- a/include/exception.h +++ b/include/exception.h @@ -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; diff --git a/src/box/box_lua.m b/src/box/box_lua.m index 6e6241f54ee0aed87a946fa5fec628693d545f38..ba9db5d42162b3c85b64f83c6984aaaff1fac3b2 100644 --- a/src/box/box_lua.m +++ b/src/box/box_lua.m @@ -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(): wrong arguments"); + uint32_t code = lua_tointeger(L, 1); + if (!code) + luaL_error(L, "box.raise(): code can't be zero"); + 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} }; diff --git a/src/exception.m b/src/exception.m index b65ff6e9467f80a0ff61a760c6c038561d876e6a..a09b6d39037a4938ca9404bf445534a80c2a1a06 100644 --- a/src/exception.m +++ b/src/exception.m @@ -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), diff --git a/test/box/raise.result b/test/box/raise.result new file mode 100644 index 0000000000000000000000000000000000000000..218d15b11f8dd7be32b67ae58778b15854625550 --- /dev/null +++ b/test/box/raise.result @@ -0,0 +1,16 @@ +lua 1 + 1 +--- + - 2 +... +lua box.raise(123, 'test') +--- +error: 'test' +... +lua box.raise(0, 'the other test') +--- +error: 'box.raise(): code can''t be zero' +... +lua box.raise(12, 345) +--- +error: '345' +... diff --git a/test/box/raise.test b/test/box/raise.test new file mode 100644 index 0000000000000000000000000000000000000000..e057d7c1e3bfba3816de301fac886ba0e029b67a --- /dev/null +++ b/test/box/raise.test @@ -0,0 +1,5 @@ +# encoding: tarantool +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)"