Permalink
Browse files

Add error handling to new methods

git-svn-id: http://luamongo.googlecode.com/svn/trunk@52 17e306f6-9439-11de-a13a-71b910e68cc8
  • Loading branch information...
1 parent 32b8cc8 commit df5d528d8e30c6bf8a5788017fd5bc2ab6d680b4 nrich@ii.net committed Aug 15, 2010
Showing with 62 additions and 44 deletions.
  1. +62 −44 mongo_connection.cpp
View
@@ -411,49 +411,26 @@ static int connection_update(lua_State *L) {
}
/*
- * __gc
- */
-static int connection_gc(lua_State *L) {
- DBClientConnection *connection = userdata_to_connection(L, 1);
- delete connection;
- return 0;
-}
-
-/*
- * __tostring
- */
-static int connection_tostring(lua_State *L) {
- DBClientConnection *connection = userdata_to_connection(L, 1);
- lua_pushfstring(L, "%s: %s", LUAMONGO_CONNECTION, connection->toString().c_str());
-
- return 1;
-}
-
-/*
- * ok, err = db:drop_collection(ns)
+ * ok,err = db:drop_collection(ns)
*/
-
static int connection_drop_collection(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
-
- bool res = connection->dropCollection(ns);
-
- if (!res) {
- string lasterr = connection->getLastError();
-
- lua_pushboolean(L, 0);
- lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION, "drop_collection", lasterr.c_str());
- return 2;
+ try {
+ connection->dropCollection(ns);
+ } catch (std::exception &e) {
+ lua_pushboolean(L, 0);
+ lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION, "drop_collection", e.what());
+ return 2;
}
lua_pushboolean(L, 1);
return 1;
}
/*
- * ok, err = db:drop_index_by_fields(ns, json_str or lua_table)
+ * ok,err = db:drop_index_by_fields(ns, json_str or lua_table)
*/
static int connection_drop_index_by_fields(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
@@ -471,6 +448,8 @@ static int connection_drop_index_by_fields(lua_State *L) {
} else {
throw(LUAMONGO_REQUIRES_JSON_OR_TABLE);
}
+
+ connection->dropIndex(ns, keys);
} catch (std::exception &e) {
lua_pushboolean(L, 0);
lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION, "drop_index_by_fields", e.what());
@@ -481,38 +460,50 @@ static int connection_drop_index_by_fields(lua_State *L) {
return 2;
}
- connection->dropIndex(ns, keys);
-
lua_pushboolean(L, 1);
return 1;
}
/*
- * db:drop_index_by_name(ns, index_name)
+ * ok,err = db:drop_index_by_name(ns, index_name)
*/
static int connection_drop_index_by_name(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
- connection->dropIndex(ns, luaL_checkstring(L, 3));
+ try {
+ connection->dropIndex(ns, luaL_checkstring(L, 3));
+ } catch (std::exception &e) {
+ lua_pushboolean(L, 0);
+ lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION, "drop_index_by_name", e.what());
+ return 2;
+ }
- return 0;
+ lua_pushboolean(L, 1);
+ return 1;
}
/*
- * db:drop_indexes(ns)
+ * ok,err = db:drop_indexes(ns)
*/
static int connection_drop_indexes(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
- connection->dropIndexes(ns);
+ try {
+ connection->dropIndexes(ns);
+ } catch (std::exception &e) {
+ lua_pushboolean(L, 0);
+ lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION, "drop_indexes", e.what());
+ return 2;
+ }
- return 0;
+ lua_pushboolean(L, 1);
+ return 1;
}
/*
- * res, err = (dbname, jscode[, args_table])
+ * res,err = (dbname, jscode[, args_table])
*/
static int connection_eval(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
@@ -633,7 +624,7 @@ static int connection_get_indexes(lua_State *L) {
}
/*
- * res, err = db:mapreduce(jsmapfunc, jsreducefunc[, query[, output]])
+ * res,err = db:mapreduce(jsmapfunc, jsreducefunc[, query[, output]])
*/
static int connection_mapreduce(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
@@ -674,15 +665,22 @@ static int connection_mapreduce(lua_State *L) {
}
/*
- * db:reindex(ns);
+ * ok,err = db:reindex(ns);
*/
static int connection_reindex(lua_State *L) {
DBClientConnection *connection = userdata_to_connection(L, 1);
const char *ns = luaL_checkstring(L, 2);
- connection->reIndex(ns);
+ try {
+ connection->reIndex(ns);
+ } catch (std::exception &e) {
+ lua_pushboolean(L, 0);
+ lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION, "reindex", e.what());
+ return 2;
+ }
- return 0;
+ lua_pushboolean(L, 1);
+ return 1;
}
/*
@@ -696,6 +694,26 @@ static int connection_reset_index_cache(lua_State *L) {
return 0;
}
+/*
+ * __gc
+ */
+static int connection_gc(lua_State *L) {
+ DBClientConnection *connection = userdata_to_connection(L, 1);
+ delete connection;
+ return 0;
+}
+
+/*
+ * __tostring
+ */
+static int connection_tostring(lua_State *L) {
+ DBClientConnection *connection = userdata_to_connection(L, 1);
+ lua_pushfstring(L, "%s: %s", LUAMONGO_CONNECTION, connection->toString().c_str());
+
+ return 1;
+}
+
+
int mongo_connection_register(lua_State *L) {
static const luaL_Reg connection_methods[] = {
{"auth", connection_auth},

0 comments on commit df5d528

Please sign in to comment.