From d3aa125d87944940080d9fe8816cd4a289d3e3a0 Mon Sep 17 00:00:00 2001 From: Dwight Date: Fri, 30 Jan 2009 14:49:28 -0500 Subject: [PATCH] implement resetError and make lasterror better --- client/dbclient.cpp | 8 ++++---- client/dbclient.h | 18 ++++++++++++------ client/examples/clientTest.cpp | 6 ++++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/client/dbclient.cpp b/client/dbclient.cpp index 1887de6bcbf..34595f021af 100644 --- a/client/dbclient.cpp +++ b/client/dbclient.cpp @@ -87,9 +87,9 @@ namespace mongo { BSONObj getlasterrorcmdobj = fromjson("{getlasterror:1}"); - string DBClientWithCommands::getLastError(const char *dbname) { + string DBClientWithCommands::getLastError() { BSONObj info; - runCommand(dbname, getlasterrorcmdobj, info); + runCommand("admin", getlasterrorcmdobj, info); BSONElement e = info["err"]; if( e.eoo() ) return ""; if( e.type() == Object ) return e.toString(); @@ -98,9 +98,9 @@ namespace mongo { BSONObj getpreverrorcmdobj = fromjson("{getpreverror:1}"); - BSONObj DBClientWithCommands::getPrevError(const char *dbname) { + BSONObj DBClientWithCommands::getPrevError() { BSONObj info; - runCommand(dbname, getpreverrorcmdobj, info); + runCommand("admin", getpreverrorcmdobj, info); return info; } diff --git a/client/dbclient.h b/client/dbclient.h index 4595214ce9d..520c759b7d1 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -281,23 +281,29 @@ namespace mongo { /** Get error result from the last operation on this connection. @return error or empty string if no error. */ - string getLastError(const char *dbname); + string getLastError(); - /* Return the last error which has occurred, even if not the very last operation. + /** Return the last error which has occurred, even if not the very last operation. - Returns: - { err : , nPrev : , ok : 1 } + @return { err : , nPrev : , ok : 1 } result.err will be null if no error has occurred. */ - BSONObj getPrevError(const char *dbname); + BSONObj getPrevError(); + + /** Reset the previous error state for this connection (accessed via getLastError and + getPrevError). Useful when performing several operations at once and then checking + for an error after attempting all operations. + */ + bool resetError() { return simpleCommand("admin", 0, "reseterror"); } /* Erase / drop an entire database */ bool dropDatabase(const char *dbname, BSONObj *info = 0) { return simpleCommand(dbname, info, "dropDatabase"); } - + + /** Delete the specified collection. */ bool dropCollection( const string ns ){ assert( ns.find( "." ) != string::npos ); int pos = ns.find( "." ); diff --git a/client/examples/clientTest.cpp b/client/examples/clientTest.cpp index ab4f8595955..8d1e36aea8c 100644 --- a/client/examples/clientTest.cpp +++ b/client/examples/clientTest.cpp @@ -112,11 +112,13 @@ int main() { assert( conn.findOne(ns, "{}")["name"].str() == "sara" ); assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" ); - assert( conn.getLastError("test") == "" ); + assert( conn.getLastError() == "" ); // nonexistent index test assert( conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")).hasElement("$err") ); - assert( conn.getLastError("test") == "hint index not found" ); + assert( conn.getLastError() == "hint index not found" ); + conn.resetError(); + assert( conn.getLastError() == "" ); //existing index assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") );