Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add. category to error class. #40

Merged
merged 2 commits into from
Apr 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/lzmq.ldoc
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
14 changes: 13 additions & 1 deletion src/lua/lzmq/ffi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/zerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@

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;
return 1;
}

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
Expand All @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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}
};
Expand Down
4 changes: 4 additions & 0 deletions test/utest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down