diff --git a/doc/lzmq.ldoc b/doc/lzmq.ldoc index 24f1b70..582a314 100644 --- a/doc/lzmq.ldoc +++ b/doc/lzmq.ldoc @@ -165,6 +165,23 @@ function error:no ()end -- @treturn string error name (e.g. "ETERM"). function error:mnemo ()end +--- Get the error name +-- This is synonym for `error:mnemo` +-- +-- @treturn string error name (e.g. "ETERM"). +function error:name ()end + +--- Get the error category +-- +-- @treturn string error category. All lzmq errors has category `ZMQ` +function error:category ()end + +--- Get the error category +-- This is synonym for `error:category` +-- +-- @treturn string error category. +function error:cat ()end + --- Get the error description -- -- @treturn string result from `zmq_strerror`(e.g. "Context was terminated"). diff --git a/src/lua/lzmq/ffi.lua b/src/lua/lzmq/ffi.lua index d611816..4344240 100644 --- a/src/lua/lzmq/ffi.lua +++ b/src/lua/lzmq/ffi.lua @@ -76,6 +76,8 @@ local function zerror(...) return Error:new(...) end do -- Error Error.__index = Error +local ERROR_CATEGORY = "ZMQ" + function Error:new(no) local o = setmetatable({ errno = no or api.zmq_errno(); @@ -94,9 +96,19 @@ end function Error:mnemo() return api.zmq_mnemoerror(self.errno) end +Error.name = Error.mnemo + +function Error:category() + return ERROR_CATEGORY +end +Error.cat = Error.category + +function Error:__eq(rhs) + return self:no() == rhs:no() +end function Error:__tostring() - return string.format("[%s] %s (%d)", self:mnemo(), self:msg(), self:no()) + return string.format("[%s][%s] %s (%d)", ERROR_CATEGORY, self:mnemo(), self:msg(), self:no()) end end diff --git a/src/zerror.c b/src/zerror.c index daaf5b6..8bfdad3 100644 --- a/src/zerror.c +++ b/src/zerror.c @@ -15,6 +15,8 @@ static const char* luazmq_err_getmnemo(int err); +#define LZMQ_ERROR_CATEGORY "ZMQ" + int luazmq_error_create(lua_State *L, int err){ zerror *zerr = luazmq_newudata(L, zerror, LUAZMQ_ERROR); zerr->no = err; @@ -22,7 +24,7 @@ int luazmq_error_create(lua_State *L, int err){ } void luazmq_error_pushstring(lua_State *L, int err){ - lua_pushfstring(L, "[%s] %s (%d)", + lua_pushfstring(L, "[" LZMQ_ERROR_CATEGORY "]""[%s] %s (%d)", luazmq_err_getmnemo(err), zmq_strerror(err), err @@ -45,6 +47,12 @@ int luazmq_assert (lua_State *L) { return lua_gettop(L); } +static int luazmq_err_cat(lua_State *L){ + luazmq_geterror(L); + lua_pushliteral(L, LZMQ_ERROR_CATEGORY); + return 1; +} + static int luazmq_err_no(lua_State *L){ zerror *zerr = luazmq_geterror(L); lua_pushinteger(L, zerr->no); @@ -69,6 +77,13 @@ static int luazmq_err_tostring(lua_State *L){ return 1; } +static int luazmq_err_equal(lua_State *L){ + zerror *lhs = luazmq_geterror_at(L, 1); + zerror *rhs = luazmq_geterror_at(L, 2); + lua_pushboolean(L, (lhs->no == rhs->no)?1:0); + return 1; +} + static const char* luazmq_err_getmnemo(int err){ #define RETURN_IF(E) case E: return #E; @@ -144,7 +159,11 @@ static const struct luaL_Reg luazmq_err_methods[] = { {"no", luazmq_err_no }, {"msg", luazmq_err_msg }, {"mnemo", luazmq_err_mnemo }, + {"name", luazmq_err_mnemo }, + {"cat", luazmq_err_cat }, + {"category", luazmq_err_cat }, {"__tostring", luazmq_err_tostring }, + {"__eq", luazmq_err_equal }, {NULL,NULL} }; diff --git a/test/utest.lua b/test/utest.lua index 678e244..4633dfc 100644 --- a/test/utest.lua +++ b/test/utest.lua @@ -204,10 +204,14 @@ function test_error() local err = zmq.error(zmq.errors.EAGAIN) assert_equal(zmq.errors.EAGAIN, err:no()) assert_equal("EAGAIN", err:mnemo()) + assert_equal("EAGAIN", err:name()) + assert_equal("ZMQ", err:cat()) + assert_equal("ZMQ", err:category()) local str_err = tostring(err) local ok, msg = pcall( zassert, false, err ) assert_false(ok) assert_equal(str_err, msg) + assert_equal(err, zmq.error(zmq.errors.EAGAIN)) end function test_interface()