webmcp
changeset 472:07e7e218d8df
Provide "__tostring" meta-method for database error objects
author | jbe |
---|---|
date | Sun May 28 00:30:10 2017 +0200 (2017-05-28) |
parents | a9fea293b2d6 |
children | 981ebb743b36 |
files | libraries/mondelefant/mondelefant_native.c |
line diff
1.1 --- a/libraries/mondelefant/mondelefant_native.c Thu May 25 02:46:23 2017 +0200 1.2 +++ b/libraries/mondelefant/mondelefant_native.c Sun May 28 00:30:10 2017 +0200 1.3 @@ -1369,6 +1369,22 @@ 1.4 return command_count+1; 1.5 } 1.6 1.7 +// meta-method "__tostring" of error objects: 1.8 +static int mondelefant_errorobject_tostring(lua_State *L) { 1.9 + const char *errclass; 1.10 + const char *errmsg; 1.11 + luaL_checktype(L, 1, LUA_TTABLE); 1.12 + lua_settop(L, 1); 1.13 + lua_getfield(L, 1, "code"); // 2 1.14 + lua_getfield(L, 1, "message"); // 3 1.15 + errclass = lua_tostring(L, 2); 1.16 + errmsg = lua_tostring(L, 3); 1.17 + if (!errclass) errclass = "(null)"; 1.18 + if (!errmsg) errclass = "(null)"; 1.19 + lua_pushfstring(L, "database error of class \"%s\": %s", errclass, errmsg); 1.20 + return 1; 1.21 +} 1.22 + 1.23 // method "is_kind_of" of error objects: 1.24 static int mondelefant_errorobject_is_kind_of(lua_State *L) { 1.25 const char *errclass; 1.26 @@ -1952,6 +1968,7 @@ 1.27 1.28 // registration information for meta-methods of error objects: 1.29 static const struct luaL_Reg mondelefant_errorobject_mt_functions[] = { 1.30 + {"__tostring", mondelefant_errorobject_tostring}, 1.31 {NULL, NULL} 1.32 }; 1.33