Skip to content

Commit fb8c79e

Browse files
committed
Merge pull request #65 from moteus/error_string_category
Change. Use string value to represent error category.
2 parents cda31a3 + b5a69f8 commit fb8c79e

File tree

4 files changed

+117
-17
lines changed

4 files changed

+117
-17
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ shallow_clone: true
77

88
environment:
99
LUAROCKS_VER: 2.2.1
10-
CURL_VER: 7.41.0
10+
CURL_VER: 7.48.0
1111

1212
matrix:
1313
- LUA_VER: 5.1.5

doc/lcurl.ldoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ do
155155

156156
--- Get the error category.
157157
--
158-
-- @treturn number number of error category (curl.ERROR_XXX constants)
158+
-- @treturn string string of error category (curl.ERROR_XXX constants)
159159
--
160160
-- @usage
161161
-- if err:category() == curl.ERROR_EASY then

src/lcerror.c

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#define LCURL_ERROR_NAME LCURL_PREFIX" Error"
1717
static const char *LCURL_ERROR = LCURL_ERROR_NAME;
1818

19+
#define LCURL_ERROR_EASY_NAME "CURL-EASY"
20+
#define LCURL_ERROR_MULTI_NAME "CURL-MULTI"
21+
#define LCURL_ERROR_SHARE_NAME "CURL-SHARE"
22+
#define LCURL_ERROR_FORM_NAME "CURL-FORM"
23+
1924
typedef struct lcurl_error_tag{
2025
int tp;
2126
int no;
@@ -89,8 +94,41 @@ static const char* _lcurl_err_msg(int tp, int err){
8994
return "<UNSUPPORTED ERROR TYPE>";
9095
}
9196

97+
static const char* _lcurl_err_category_name(int tp){
98+
assert(
99+
(tp == LCURL_ERROR_EASY ) ||
100+
(tp == LCURL_ERROR_MULTI) ||
101+
(tp == LCURL_ERROR_SHARE) ||
102+
(tp == LCURL_ERROR_FORM ) ||
103+
0
104+
);
105+
106+
switch(tp){
107+
case LCURL_ERROR_EASY: {
108+
static const char *name = LCURL_ERROR_EASY_NAME;
109+
return name;
110+
}
111+
case LCURL_ERROR_MULTI: {
112+
static const char *name = LCURL_ERROR_MULTI_NAME;
113+
return name;
114+
}
115+
case LCURL_ERROR_SHARE: {
116+
static const char *name = LCURL_ERROR_SHARE_NAME;
117+
return name;
118+
}
119+
case LCURL_ERROR_FORM: {
120+
static const char *name = LCURL_ERROR_FORM_NAME;
121+
return name;
122+
}
123+
}
124+
125+
assert(0);
126+
return NULL;
127+
}
128+
92129
static void _lcurl_err_pushstring(lua_State *L, int tp, int err){
93-
lua_pushfstring(L, "[%s] %s (%d)",
130+
lua_pushfstring(L, "[%s][%s] %s (%d)",
131+
_lcurl_err_category_name(tp),
94132
_lcurl_err_mnemo(tp, err),
95133
_lcurl_err_msg(tp, err),
96134
err
@@ -158,7 +196,7 @@ static int lcurl_err_equal(lua_State *L){
158196

159197
static int lcurl_err_category(lua_State *L){
160198
lcurl_error_t *err = lcurl_geterror(L);
161-
lua_pushinteger(L, err->tp);
199+
lua_pushstring(L, _lcurl_err_category_name(err->tp));
162200
return 1;
163201
}
164202

@@ -190,9 +228,30 @@ int lcurl_fail(lua_State *L, int error_type, int code){
190228

191229
//}
192230

231+
static const int ERROR_CATEGORIES[] = {
232+
LCURL_ERROR_EASY,
233+
LCURL_ERROR_MULTI,
234+
LCURL_ERROR_SHARE,
235+
LCURL_ERROR_FORM,
236+
};
237+
238+
static const char* ERROR_CATEGORIES_NAME[] = {
239+
LCURL_ERROR_EASY_NAME,
240+
LCURL_ERROR_MULTI_NAME,
241+
LCURL_ERROR_SHARE_NAME,
242+
LCURL_ERROR_FORM_NAME,
243+
NULL
244+
};
245+
193246
int lcurl_error_new(lua_State *L){
194-
int tp = luaL_checkint(L, 1);
195-
int no = luaL_checkint(L, 2);
247+
int tp, no = luaL_checkint(L, 2);
248+
if (lua_isnumber(L, 1)){
249+
tp = luaL_checkint(L, 2);
250+
}
251+
else{
252+
tp = luaL_checkoption(L, 1, NULL, ERROR_CATEGORIES_NAME);
253+
tp = ERROR_CATEGORIES[tp];
254+
}
196255

197256
//! @todo checks error type value
198257

@@ -234,21 +293,15 @@ static const lcurl_const_t lcurl_error_codes[] = {
234293
{NULL, 0}
235294
};
236295

237-
static const lcurl_const_t lcurl_error_category[] = {
238-
{"ERROR_CURL", LCURL_ERROR_CURL},
239-
{"ERROR_EASY", LCURL_ERROR_EASY},
240-
{"ERROR_MULTI", LCURL_ERROR_MULTI},
241-
{"ERROR_SHARE", LCURL_ERROR_SHARE},
242-
{"ERROR_FORM", LCURL_ERROR_FORM},
243-
244-
{NULL, 0}
245-
};
246-
247296
void lcurl_error_initlib(lua_State *L, int nup){
248297
if(!lutil_createmetap(L, LCURL_ERROR, lcurl_err_methods, nup))
249298
lua_pop(L, nup);
250299
lua_pop(L, 1);
251300

252301
lcurl_util_set_const(L, lcurl_error_codes);
253-
lcurl_util_set_const(L, lcurl_error_category);
302+
303+
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_EASY ));lua_setfield(L, -2, "ERROR_EASY" );
304+
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_MULTI ));lua_setfield(L, -2, "ERROR_MULTI");
305+
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_SHARE ));lua_setfield(L, -2, "ERROR_SHARE");
306+
lua_pushstring(L, _lcurl_err_category_name(LCURL_ERROR_FORM ));lua_setfield(L, -2, "ERROR_FORM" );
254307
}

test/test_easy.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,53 @@ end
8989

9090
local ENABLE = true
9191

92+
local _ENV = TEST_CASE'curl error' if ENABLE then
93+
94+
function test_eq_with_same_cat()
95+
local e1 = curl.error(curl.ERROR_EASY, curl.E_OK)
96+
local e2 = curl.error(curl.ERROR_EASY, curl.E_OK)
97+
assert_equal(e1, e2)
98+
end
99+
100+
function test_eq_with_different_cat()
101+
local e1 = curl.error(curl.ERROR_EASY, curl.E_OK)
102+
local e2 = curl.error(curl.ERROR_FORM, curl.E_OK)
103+
104+
assert_equal(e1:no(), e2:no())
105+
assert_not_equal(e1, e2)
106+
end
107+
108+
function test_ctor_cat()
109+
local e
110+
111+
e = curl.error(curl.ERROR_EASY, curl.E_OK)
112+
assert_equal(e:category(), curl.ERROR_EASY)
113+
assert_equal(e:no(), curl.E_OK)
114+
115+
e = curl.error(curl.ERROR_MULTI, curl.E_OK)
116+
assert_equal(e:category(), curl.ERROR_MULTI)
117+
assert_equal(e:no(), curl.E_OK)
118+
119+
e = curl.error(curl.ERROR_SHARE, curl.E_OK)
120+
assert_equal(e:category(), curl.ERROR_SHARE)
121+
assert_equal(e:no(), curl.E_OK)
122+
123+
e = curl.error(curl.ERROR_FORM, curl.E_OK)
124+
assert_equal(e:category(), curl.ERROR_FORM)
125+
assert_equal(e:no(), curl.E_OK)
126+
127+
assert_error(function()
128+
curl.error(nil, curl.E_OK)
129+
end)
130+
131+
assert_error(function()
132+
curl.error('UNKNOWN STRING', curl.E_OK)
133+
end)
134+
135+
end
136+
137+
end
138+
92139
local _ENV = TEST_CASE'write_callback' if ENABLE then
93140

94141
local c, f

0 commit comments

Comments
 (0)