Merge pull request #65 from moteus/error_string_category

Change. Use string value to represent error category.
This commit is contained in:
Alexey Melnichuk 2016-04-15 22:25:43 +03:00
commit fb8c79e89f
4 changed files with 117 additions and 17 deletions

View File

@ -7,7 +7,7 @@ shallow_clone: true
environment:
LUAROCKS_VER: 2.2.1
CURL_VER: 7.41.0
CURL_VER: 7.48.0
matrix:
- LUA_VER: 5.1.5

View File

@ -155,7 +155,7 @@ do
--- Get the error category.
--
-- @treturn number number of error category (curl.ERROR_XXX constants)
-- @treturn string string of error category (curl.ERROR_XXX constants)
--
-- @usage
-- if err:category() == curl.ERROR_EASY then

View File

@ -16,6 +16,11 @@
#define LCURL_ERROR_NAME LCURL_PREFIX" Error"
static const char *LCURL_ERROR = LCURL_ERROR_NAME;
#define LCURL_ERROR_EASY_NAME "CURL-EASY"
#define LCURL_ERROR_MULTI_NAME "CURL-MULTI"
#define LCURL_ERROR_SHARE_NAME "CURL-SHARE"
#define LCURL_ERROR_FORM_NAME "CURL-FORM"
typedef struct lcurl_error_tag{
int tp;
int no;
@ -89,8 +94,41 @@ static const char* _lcurl_err_msg(int tp, int err){
return "<UNSUPPORTED ERROR TYPE>";
}
static const char* _lcurl_err_category_name(int tp){
assert(
(tp == LCURL_ERROR_EASY ) ||
(tp == LCURL_ERROR_MULTI) ||
(tp == LCURL_ERROR_SHARE) ||
(tp == LCURL_ERROR_FORM ) ||
0
);
switch(tp){
case LCURL_ERROR_EASY: {
static const char *name = LCURL_ERROR_EASY_NAME;
return name;
}
case LCURL_ERROR_MULTI: {
static const char *name = LCURL_ERROR_MULTI_NAME;
return name;
}
case LCURL_ERROR_SHARE: {
static const char *name = LCURL_ERROR_SHARE_NAME;
return name;
}
case LCURL_ERROR_FORM: {
static const char *name = LCURL_ERROR_FORM_NAME;
return name;
}
}
assert(0);
return NULL;
}
static void _lcurl_err_pushstring(lua_State *L, int tp, int err){
lua_pushfstring(L, "[%s] %s (%d)",
lua_pushfstring(L, "[%s][%s] %s (%d)",
_lcurl_err_category_name(tp),
_lcurl_err_mnemo(tp, err),
_lcurl_err_msg(tp, err),
err
@ -158,7 +196,7 @@ static int lcurl_err_equal(lua_State *L){
static int lcurl_err_category(lua_State *L){
lcurl_error_t *err = lcurl_geterror(L);
lua_pushinteger(L, err->tp);
lua_pushstring(L, _lcurl_err_category_name(err->tp));
return 1;
}
@ -190,9 +228,30 @@ int lcurl_fail(lua_State *L, int error_type, int code){
//}
static const int ERROR_CATEGORIES[] = {
LCURL_ERROR_EASY,
LCURL_ERROR_MULTI,
LCURL_ERROR_SHARE,
LCURL_ERROR_FORM,
};
static const char* ERROR_CATEGORIES_NAME[] = {
LCURL_ERROR_EASY_NAME,
LCURL_ERROR_MULTI_NAME,
LCURL_ERROR_SHARE_NAME,
LCURL_ERROR_FORM_NAME,
NULL
};
int lcurl_error_new(lua_State *L){
int tp = luaL_checkint(L, 1);
int no = luaL_checkint(L, 2);
int tp, no = luaL_checkint(L, 2);
if (lua_isnumber(L, 1)){
tp = luaL_checkint(L, 2);
}
else{
tp = luaL_checkoption(L, 1, NULL, ERROR_CATEGORIES_NAME);
tp = ERROR_CATEGORIES[tp];
}
//! @todo checks error type value
@ -234,21 +293,15 @@ static const lcurl_const_t lcurl_error_codes[] = {
{NULL, 0}
};
static const lcurl_const_t lcurl_error_category[] = {
{"ERROR_CURL", LCURL_ERROR_CURL},
{"ERROR_EASY", LCURL_ERROR_EASY},
{"ERROR_MULTI", LCURL_ERROR_MULTI},
{"ERROR_SHARE", LCURL_ERROR_SHARE},
{"ERROR_FORM", LCURL_ERROR_FORM},
{NULL, 0}
};
void lcurl_error_initlib(lua_State *L, int nup){
if(!lutil_createmetap(L, LCURL_ERROR, lcurl_err_methods, nup))
lua_pop(L, nup);
lua_pop(L, 1);
lcurl_util_set_const(L, lcurl_error_codes);
lcurl_util_set_const(L, lcurl_error_category);
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_EASY ));lua_setfield(L, -2, "ERROR_EASY" );
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_MULTI ));lua_setfield(L, -2, "ERROR_MULTI");
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_SHARE ));lua_setfield(L, -2, "ERROR_SHARE");
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_FORM ));lua_setfield(L, -2, "ERROR_FORM" );
}

View File

@ -89,6 +89,53 @@ end
local ENABLE = true
local _ENV = TEST_CASE'curl error' if ENABLE then
function test_eq_with_same_cat()
local e1 = curl.error(curl.ERROR_EASY, curl.E_OK)
local e2 = curl.error(curl.ERROR_EASY, curl.E_OK)
assert_equal(e1, e2)
end
function test_eq_with_different_cat()
local e1 = curl.error(curl.ERROR_EASY, curl.E_OK)
local e2 = curl.error(curl.ERROR_FORM, curl.E_OK)
assert_equal(e1:no(), e2:no())
assert_not_equal(e1, e2)
end
function test_ctor_cat()
local e
e = curl.error(curl.ERROR_EASY, curl.E_OK)
assert_equal(e:category(), curl.ERROR_EASY)
assert_equal(e:no(), curl.E_OK)
e = curl.error(curl.ERROR_MULTI, curl.E_OK)
assert_equal(e:category(), curl.ERROR_MULTI)
assert_equal(e:no(), curl.E_OK)
e = curl.error(curl.ERROR_SHARE, curl.E_OK)
assert_equal(e:category(), curl.ERROR_SHARE)
assert_equal(e:no(), curl.E_OK)
e = curl.error(curl.ERROR_FORM, curl.E_OK)
assert_equal(e:category(), curl.ERROR_FORM)
assert_equal(e:no(), curl.E_OK)
assert_error(function()
curl.error(nil, curl.E_OK)
end)
assert_error(function()
curl.error('UNKNOWN STRING', curl.E_OK)
end)
end
end
local _ENV = TEST_CASE'write_callback' if ENABLE then
local c, f