# HG changeset patch # User jbe # Date 1495924210 -7200 # Node ID 07e7e218d8df6d9869011060f80f6480f8801f9e # Parent a9fea293b2d6830174108d29b0eddc86eb88eba3 Provide "__tostring" meta-method for database error objects diff -r a9fea293b2d6 -r 07e7e218d8df libraries/mondelefant/mondelefant_native.c --- a/libraries/mondelefant/mondelefant_native.c Thu May 25 02:46:23 2017 +0200 +++ b/libraries/mondelefant/mondelefant_native.c Sun May 28 00:30:10 2017 +0200 @@ -1369,6 +1369,22 @@ return command_count+1; } +// meta-method "__tostring" of error objects: +static int mondelefant_errorobject_tostring(lua_State *L) { + const char *errclass; + const char *errmsg; + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 1); + lua_getfield(L, 1, "code"); // 2 + lua_getfield(L, 1, "message"); // 3 + errclass = lua_tostring(L, 2); + errmsg = lua_tostring(L, 3); + if (!errclass) errclass = "(null)"; + if (!errmsg) errclass = "(null)"; + lua_pushfstring(L, "database error of class \"%s\": %s", errclass, errmsg); + return 1; +} + // method "is_kind_of" of error objects: static int mondelefant_errorobject_is_kind_of(lua_State *L) { const char *errclass; @@ -1952,6 +1968,7 @@ // registration information for meta-methods of error objects: static const struct luaL_Reg mondelefant_errorobject_mt_functions[] = { + {"__tostring", mondelefant_errorobject_tostring}, {NULL, NULL} };