|
16 | 16 | #define LCURL_ERROR_NAME LCURL_PREFIX" Error" |
17 | 17 | static const char *LCURL_ERROR = LCURL_ERROR_NAME; |
18 | 18 |
|
| 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 | + |
19 | 24 | typedef struct lcurl_error_tag{ |
20 | 25 | int tp; |
21 | 26 | int no; |
@@ -89,8 +94,41 @@ static const char* _lcurl_err_msg(int tp, int err){ |
89 | 94 | return "<UNSUPPORTED ERROR TYPE>"; |
90 | 95 | } |
91 | 96 |
|
| 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 | + |
92 | 129 | 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), |
94 | 132 | _lcurl_err_mnemo(tp, err), |
95 | 133 | _lcurl_err_msg(tp, err), |
96 | 134 | err |
@@ -158,7 +196,7 @@ static int lcurl_err_equal(lua_State *L){ |
158 | 196 |
|
159 | 197 | static int lcurl_err_category(lua_State *L){ |
160 | 198 | lcurl_error_t *err = lcurl_geterror(L); |
161 | | - lua_pushinteger(L, err->tp); |
| 199 | + lua_pushstring(L, _lcurl_err_category_name(err->tp)); |
162 | 200 | return 1; |
163 | 201 | } |
164 | 202 |
|
@@ -190,9 +228,30 @@ int lcurl_fail(lua_State *L, int error_type, int code){ |
190 | 228 |
|
191 | 229 | //} |
192 | 230 |
|
| 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 | + |
193 | 246 | 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 | + } |
196 | 255 |
|
197 | 256 | //! @todo checks error type value |
198 | 257 |
|
@@ -234,21 +293,15 @@ static const lcurl_const_t lcurl_error_codes[] = { |
234 | 293 | {NULL, 0} |
235 | 294 | }; |
236 | 295 |
|
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 | | - |
247 | 296 | void lcurl_error_initlib(lua_State *L, int nup){ |
248 | 297 | if(!lutil_createmetap(L, LCURL_ERROR, lcurl_err_methods, nup)) |
249 | 298 | lua_pop(L, nup); |
250 | 299 | lua_pop(L, 1); |
251 | 300 |
|
252 | 301 | 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" ); |
254 | 307 | } |
0 commit comments