0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

implement resetError

and make lasterror better
This commit is contained in:
Dwight 2009-01-30 14:49:28 -05:00
parent 790ef16736
commit d3aa125d87
3 changed files with 20 additions and 12 deletions

View File

@ -87,9 +87,9 @@ namespace mongo {
BSONObj getlasterrorcmdobj = fromjson("{getlasterror:1}"); BSONObj getlasterrorcmdobj = fromjson("{getlasterror:1}");
string DBClientWithCommands::getLastError(const char *dbname) { string DBClientWithCommands::getLastError() {
BSONObj info; BSONObj info;
runCommand(dbname, getlasterrorcmdobj, info); runCommand("admin", getlasterrorcmdobj, info);
BSONElement e = info["err"]; BSONElement e = info["err"];
if( e.eoo() ) return ""; if( e.eoo() ) return "";
if( e.type() == Object ) return e.toString(); if( e.type() == Object ) return e.toString();
@ -98,9 +98,9 @@ namespace mongo {
BSONObj getpreverrorcmdobj = fromjson("{getpreverror:1}"); BSONObj getpreverrorcmdobj = fromjson("{getpreverror:1}");
BSONObj DBClientWithCommands::getPrevError(const char *dbname) { BSONObj DBClientWithCommands::getPrevError() {
BSONObj info; BSONObj info;
runCommand(dbname, getpreverrorcmdobj, info); runCommand("admin", getpreverrorcmdobj, info);
return info; return info;
} }

View File

@ -281,23 +281,29 @@ namespace mongo {
/** Get error result from the last operation on this connection. /** Get error result from the last operation on this connection.
@return error or empty string if no error. @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: @return { err : <error message>, nPrev : <how_many_ops_back_occurred>, ok : 1 }
{ err : <error message>, nPrev : <how_many_ops_back_occurred>, ok : 1 }
result.err will be null if no error has occurred. 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 */ /* Erase / drop an entire database */
bool dropDatabase(const char *dbname, BSONObj *info = 0) { bool dropDatabase(const char *dbname, BSONObj *info = 0) {
return simpleCommand(dbname, info, "dropDatabase"); return simpleCommand(dbname, info, "dropDatabase");
} }
/** Delete the specified collection. */
bool dropCollection( const string ns ){ bool dropCollection( const string ns ){
assert( ns.find( "." ) != string::npos ); assert( ns.find( "." ) != string::npos );
int pos = ns.find( "." ); int pos = ns.find( "." );

View File

@ -112,11 +112,13 @@ int main() {
assert( conn.findOne(ns, "{}")["name"].str() == "sara" ); assert( conn.findOne(ns, "{}")["name"].str() == "sara" );
assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" ); assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" );
assert( conn.getLastError("test") == "" ); assert( conn.getLastError() == "" );
// nonexistent index test // nonexistent index test
assert( conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")).hasElement("$err") ); 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 //existing index
assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") ); assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") );